?
Solved

Character and char

Posted on 2001-06-17
4
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
venkat2000120699 earned 20 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

770 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