From: James Westby on 10 Jan 2006 16:09 James Westby wrote: > haig wrote: > >> "VisionSet" <spam(a)ntlworld.com> wrote in news:WOUwf.32187$yu.5572 >> @newsfe6-gui.ntli.net: >> >> >> >>> word.charAt(i) returns a char primitive, you can not call methods on a >>> primitive ie equals(String str) >>> For that matter you must make the two objects of the same type to make >>> equals meaningful. >>> >>> so >>> >>> objectOneOfTypeA.equals(objectTwoOfTypeA) // is okay >>> >>> to modify your example >>> >>> char chrPrim = word.charAt(i); >>> Character chrObject = Character.valueOf(chrPrim); >>> boolean isEqual = Character.valueOf('a').equals(chrObject); >>> >>> but since you have a primitive it is easier to just do >>> >>> if ( word.charAt(i) == 'a' ) {...} // !! >>> >> >> >> Thanks >> >> And if I want to compare a string of vouwels >> String [] vouwels = {"A", "a", "E", "e", "U", "u", "I", "i", "O", "o"}; >> if(word.charAt(i) == vouwels[j]){} //? >> >> So I need to count the vouwels in a word... >> > With proper looping and counting that could do it, yes. Take a look at > .toCharArray() method of string, to save repeatedly extracting the same > characters out of the string, then it's just a case of looping over the > two arrays and incrementing a count when the values match. > > James As Roedy pointed out you need a char[] not String[] of vowels. This applies to this method as well. James
From: Thomas Hawtin on 10 Jan 2006 16:27 Thomas Fritsch wrote: > "haig" <haigremove(a)pandora.be> wrote: > >>And if I want to compare a string of vouwels >> >>String [] vouwels = {"A", "a", "E", "e", "U", "u", "I", "i", "O", "o"}; >> >>if(word.charAt(i) == vouwels[j]){} //? >> >>So I need to count the vouwels in a word... >> > > The compiler will give an error, because you try to compare char with > String. > > What you probably want to compare char wit char: > char [] vouwels = {'A', 'a', 'E', 'e', 'U', 'u', 'I', 'i', 'O', 'o'}; Or, easier to read: char[] vowels = "AaEeIiOoUu".toCharArray(); Tom Hawtin -- Unemployed English Java programmer http://jroller.com/page/tackline/
From: Roedy Green on 10 Jan 2006 16:30 On Tue, 10 Jan 2006 21:54:09 +0100, Malte Christensen <You_can_spam_me_here(a)nmalte.dk> wrote, quoted or indirectly quoted someone who said : >Seems that charAt() returns a char. A char does not have a method named >equals. In fact, because char is a primitive, it does not have ANY methods. Only Objects have methods, e.g. Character or String. -- Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
From: Mike Schilling on 10 Jan 2006 21:52 "Roedy Green" <my_email_is_posted_on_my_website(a)munged.invalid> wrote in message news:pf78s11h1nf7iafoiu4t0ghcoc42liql26(a)4ax.com... > On Tue, 10 Jan 2006 20:39:36 GMT, haig <haigremove(a)pandora.be> wrote, > quoted or indirectly quoted someone who said : > >>String [] vouwels = {"A", "a", "E", "e", "U", "u", "I", "i", "O", "o"}; >> >>if(word.charAt(i) == vouwels[j]){} //? > > You would need a nested loop over the chars in word and the possible > vowels and increment a counter when you find a match. > > The for:each is neat for this: > > for ( char vowel : vowels ) > > But you need to use a char[] instead of a String[]. Simpler, though is: int index = "AaEeIiOoUu".indexOf (word.charAt(i)); if (index >= 0) ...
From: Malte Christensen on 11 Jan 2006 10:03 Roedy Green wrote: > On Tue, 10 Jan 2006 21:54:09 +0100, Malte Christensen > <You_can_spam_me_here(a)nmalte.dk> wrote, quoted or indirectly quoted > someone who said : > > >>Seems that charAt() returns a char. A char does not have a method named >>equals. > > > In fact, because char is a primitive, it does not have ANY methods. > Only Objects have methods, e.g. Character or String. Tnx for stating the obvious, I had left it to the OP.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Unable to passivate due to ctx lock warning on JBoss 4.0.2 Next: EXCEPTION_ACCESS_VIOLATION |