From: James Westby on 10 Jan 2006 15:51 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
From: Roedy Green on 10 Jan 2006 15:52 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[]. -- Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
From: Malte Christensen on 10 Jan 2006 15:54 haig wrote: > Hello > > Ik get an error on this piece of code: > > if((word.charAt(i)).equals("a"){ > .... > } > > Error: char cannot be dereferenced > > Can someone tell me what's wrong? Or how can I compare each letter of the > word to the "a"? > > Thanks Seems that charAt() returns a char. A char does not have a method named equals. Try if (word.charAt(i) == 'a') { .... }
From: Roedy Green on 10 Jan 2006 15:57 On Tue, 10 Jan 2006 20:18:26 GMT, haig <haigremove(a)pandora.be> wrote, quoted or indirectly quoted someone who said : >if((word.charAt(i)).equals("a"){ compare Strings with Strings and chars with chars. You have char on the left and String on the right. -- Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
From: Thomas Fritsch on 10 Jan 2006 15:58 "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'}; if(word.charAt(i) == vouwels[j]){} -- "TFritsch$t-online:de".replace(':','.').replace('$','@')
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 |