Link to home
Start Free TrialLog in
Avatar of JYnet
JYnet

asked on

Character and char

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?

ASKER CERTIFIED SOLUTION
Avatar of venkat2000120699
venkat2000120699

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of venkat2000120699
venkat2000120699

To compare both, use something like,
if (chObj.charValue() == 'a') System.out.println("equal");
//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);
    }
}
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.