Computing.Net > Forums > Programming > how do you compare 'char' in java

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

how do you compare 'char' in java

Reply to Message Icon

Name: Sanjay11
Date: October 20, 2002 at 09:09:53 Pacific
OS: windows98
CPU/Ram: pentium2, 64
Comment:

Im trying to compare two 'char' datatype in java, and am having problems, ive tried equals() and compareTo(), both dont work, any help would be gratly appreciated.

Thanks in advance,
Pamela



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: October 20, 2002 at 09:51:42 Pacific
Reply:

I'm pretty new to Java and have not used char, but my book lists it as a Numeric Data Type, along with Byte, Short, Int, etc.

Have you tried ==?


0

Response Number 2
Name: Blake Ellison
Date: October 20, 2002 at 10:05:10 Pacific
Reply:

From what I can see .equal() and .compareTo() are the only ways to compare chars. How are putting them into your if or loop statement?


0

Response Number 3
Name: Tom
Date: October 20, 2002 at 17:51:55 Pacific
Reply:

They are for Strings, not chars. == should work fine, as a char is really a numeric data type.


0

Response Number 4
Name: Blake Ellison
Date: October 21, 2002 at 08:53:28 Pacific
Reply:

I do not think == would work because it is only comparing if there is the same number of letters, it does not distinguish between individual letters. For example the == would think that 'from' and 'form' where equal with each other.


0

Response Number 5
Name: Don Arnett
Date: October 21, 2002 at 09:27:27 Pacific
Reply:

"Teach Yourself JAVA in 21 Days, Professional Edition", page 60

"The char type is used for individual characters."

"from" or "form" would have to be a String, char array, charBuffer or some such.

Additionally, the only situation where 'from' and 'form' would be considered equal would be if you were comparing 'sets' of characters.


0

Related Posts

See More



Response Number 6
Name: SN
Date: October 21, 2002 at 23:10:42 Pacific
Reply:

If you are able to fit either 'form' or 'from' into a char type in java, you don't have to learn how to program because you will get rich showing the world how to fit 32 bits of data into 8 bits.

char is a primitive data type, and will hold just one character. The == operator will work beatifully as it will compare the ascii codes of the operands and let you know if they are equal, exactly as it does with ints.

If you desire to compare things like "form" and "from", you should declare them as Strings, not chars, and use statements like
"from".compareTo("form"); and you will get a proper comparison. Good luck,
SN


0

Response Number 7
Name: Orlando
Date: October 31, 2002 at 13:04:32 Pacific
Reply:

I am interested because I have the same problem.

I am parsing a String that has commas and about to know when I have a comma.

I have this code that does not work:

char c = myString.charAt(J);

if (c == ",")
counter ++;

Sorry, It did work I just change
from
if (c == "," )
to
if (c == ',')


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: how do you compare 'char' in java

How do you skip lines in msgbox VB www.computing.net/answers/programming/how-do-you-skip-lines-in-msgbox-vb/3864.html

how do use call function in java like C www.computing.net/answers/programming/how-do-use-call-function-in-java-like-c/951.html

How do you get flash? www.computing.net/answers/programming/how-do-you-get-flash/16013.html