Computer Science Canada is c++'s != the same in java? |
Author: | Justin_ [ Wed Mar 01, 2006 7:38 pm ] | ||
Post subject: | is c++'s != the same in java? | ||
can someone tell me why it doesnt go back to the loop after try again? I know it wont try the whole thing again due to the breadth of the while loop, but it should do the while loop again. Unless the != (does not equal) sign is different in java. |
Author: | wtd [ Wed Mar 01, 2006 8:12 pm ] | ||
Post subject: | |||
Java's == and != operators work the way you'd expect... for "native" types. For object types they act referentially.
Only is true if they're actually the same object. If you wish to test for structural equality, use the equals method. |
Author: | Andy [ Wed Mar 01, 2006 8:35 pm ] |
Post subject: | |
in c++ != and == will return u 0 or 1, in java, true != 1 and false != 0 |
Author: | Justin_ [ Wed Mar 01, 2006 9:32 pm ] |
Post subject: | |
I'm begining to suspect that the readLine() method does something funky. observe: playAgain = input.readLine(); while (playAgain == "yes") { execute this } okay, so if i type in "yes" without quotes, it should do the while loop right? |
Author: | Justin_ [ Wed Mar 01, 2006 9:37 pm ] | ||
Post subject: | |||
Please tell me why it doesnt repeat itself if you type: yes after you guessed the number correct.
|
Author: | Andy [ Wed Mar 01, 2006 9:54 pm ] |
Post subject: | |
two suggestions, check if the string you're comparing with "yes" is indeed just "yes" and no endline character, output the length of the string, and check each character. Also, make sure you're using Java 1.50 if you're going to do that, if not you'll have to use string.equals |
Author: | wtd [ Wed Mar 01, 2006 10:17 pm ] |
Post subject: | |
I've given you the answer. If you wish to strip off any extra spaces (newline or otherwise), use the trim method. |
Author: | Justin_ [ Wed Mar 01, 2006 10:39 pm ] |
Post subject: | |
I output the length of the word yes and it returns 3. Also I read that the readLine() method does not include the terminating characters or anything like that when writing to a string variable. wtd, if you've given the answer then I don't understand it. A trim method will not help in this situation considering the string is just "yes" so there has to be something else wrong with my program. Whoever can tell me what earns a cookie |
Author: | wtd [ Wed Mar 01, 2006 11:25 pm ] |
Post subject: | |
Strings are objects in Java. There's your answer. |
Author: | Justin_ [ Wed Mar 01, 2006 11:32 pm ] |
Post subject: | |
oh indeed, thanks so much. When you say equals method I tried the statement with one equal sign lol. But now I understand, thanks. |
Author: | [Gandalf] [ Thu Mar 02, 2006 11:28 pm ] |
Post subject: | |
Justin_ wrote: hi
Eh? Would I be correct in the assumption that someone has commandeered your account? |