Solved

Character and char

Posted on 2001-06-17
4
235 Views
Last Modified: 2010-03-31
just wondering how to compare(convert) the Character datatype to char datatype?

i store Character inside a Stack and want to compare the content of the Stack with char.

how this can be done?

0
Comment
Question by:JYnet
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
venkat2000120699 earned 5 total points
ID: 6200708
To get char value stored in Character object, use, charValue() method.

eg.;  Character chObj = new Character('a');
      char c = chObj.charValue();
      System.out.println("character:" + c);
0
 
LVL 2

Expert Comment

by:venkat2000120699
ID: 6200711
To compare both, use something like,
if (chObj.charValue() == 'a') System.out.println("equal");
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6201341
//where stack is your Stack object
myChar char = "a";
for (int i = 0; i < stack.size(); i++){
    if (((Character)stack.elementAt(i)).charValue()== 'a'){
        System.out.println("Character " + i + " in the stack is equal to " + myChar);
    }
}
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6201348
Damn, sorry...that should have been :

for (int i = 0; i < stack.size(); i++){
   if (((Character)stack.elementAt(i)).charValue()== myChar){
       System.out.println("Character " + i + " in the stack is equal to " + myChar);
   }
}

Basically you iterate through the Stack getting each object and casting it to a Character (since all items in a Stack are upcast to Object) and then comapring it to a char.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now