Solved

Character and char

Posted on 2001-06-17
4
238 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 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

730 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