From: haig on 10 Jan 2006 15:18 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
From: James Westby on 10 Jan 2006 15:25 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 Try ..equals('a'){ becomes == 'a'){ James
From: VisionSet on 10 Jan 2006 15:29 "haig" <haigremove(a)pandora.be> wrote in message news:mEUwf.93178$JO4.6047840(a)phobos.telenet-ops.be... > Hello > > Ik get an error on this piece of code: > > if((word.charAt(i)).equals("a"){ > .... > } > > Error: char cannot be dereferenced 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' ) {...} // !! -- Mike W
From: haig on 10 Jan 2006 15:39 "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...
From: Roedy Green on 10 Jan 2006 15:49 On Tue, 10 Jan 2006 20:25:45 GMT, James Westby <jw2328(a)bris.ac.uk> wrote, quoted or indirectly quoted someone who said : >> if((word.charAt(i)).equals("a"){ You have two problems. equals is for comparing objects; == is for comparing primitives. You have primitives. Secondly your () don't balance. -- Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
|
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 |