Solved

Character and char

Posted on 2001-06-17
4
234 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
Comment Utility
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
Comment Utility
To compare both, use something like,
if (chObj.charValue() == 'a') System.out.println("equal");
0
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
//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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strDist challenge 35 84
mapBully challenge 6 88
Running Jira on Raspberry PI 2? 3 121
Java - Why doesn't this JFrame work 3 15
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

16 Experts available now in Live!

Get 1:1 Help Now