![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Sexy Programmer
|
Comparing characters from words
Hey, I am making this program where it has to compare a character form a word to another character. This is my code so far. I keep getting error messages saying "Cannot invoke .equals on the primitive type char"
[PHP] public static int compareChar(char a, String word) { for(int c = 0; c < word.length(); c++) { if(word.charAt(c).equals(a)) return c; } return -1; } [/PHP] Can someone help me out? I even tried doing this [PHP] public static int compareChar(char a, String word) { for(int c = 0; c < word.length(); c++) { char x = word.charAt(c) if(x.equals(a)) return c; } return -1; } [/PHP] Should I try just comparing them using the compareTo method instead of check for equality?
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
How about just using ==
|
|
|
|
|
|
#3 |
|
Sexy Programmer
|
doesn't that just compare if the characters are part of the same object?
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
for primitive types (int, char, double, ...) == compares the value. For anything that extends Object (meaning anything else), == compares the memory location.
|
|
|
|
|
|
#5 |
|
Sexy Programmer
|
I shouldn't have over-looked the power of characters in ANY language,
thanks!
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Maybe I'm stupid, but aren't you trying to do the same as indexOf()?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#7 |
|
Sexy Programmer
|
omg nnxion, I completely forgot about that string method! That's a big help, thanks man! lol
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#8 |
|
Expert Programmer
|
Are you using the comparable interface? if not then do this
public class test implements Comparable
{
//whatever you need to put here
// a.k.a your code
}
__________________
|
|
|
|
|
|
#9 |
|
Sexy Programmer
|
I've never heard of that interface. I will look up the methods online. Sounds and Looks promising.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#10 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Comparable is fine for classes, but still won't work for primitives...
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|