Results 1 to 9 of 9
Thread: Problem with if statement - Char
- 12-20-2009, 08:21 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Problem with if statement - Char
Hey all.
I'm trying to write an IF statement that compares 3 characters: s, d and t with a user entered character.
Here's what i've got:
Java Code:character = TextIO.getlnChar; if(character != 's' && character != 'd' && character != 't'){ valid = false } else{ valid = true }
Thanks for the help.
MikeLast edited by KardKaper; 12-20-2009 at 08:24 PM.
- 12-20-2009, 08:29 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 12-21-2009, 12:42 PM #3
Member
- Join Date
- Dec 2009
- Location
- Rio de Janeiro
- Posts
- 38
- Rep Power
- 0
Also remember that 's' != 'S'... it's case sensitive.
Please don't laugh at my English... I'm trying my best! :)
- 12-21-2009, 01:29 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
not sure if this matters, but, you should prob use the || boolean or operator.... one constant cant equal three constants....
Java Code:if(character != 's' || 'd' || 't') {thing = true;}else{ thing = false}
Last edited by helpisontheway; 12-21-2009 at 01:32 PM.
- 12-21-2009, 01:45 PM #5
Member
- Join Date
- Dec 2009
- Location
- Rio de Janeiro
- Posts
- 38
- Rep Power
- 0
That doesn't work with Java!
Please don't laugh at my English... I'm trying my best! :)
- 12-21-2009, 02:10 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 12-21-2009, 02:21 PM #7
Member
- Join Date
- Dec 2009
- Location
- Rio de Janeiro
- Posts
- 38
- Rep Power
- 0
JosAH
I thought it didn't work with C++ but I tried it in Eclipse before posting and it worked.
I think it doesn't work with C.Please don't laugh at my English... I'm trying my best! :)
- 12-21-2009, 02:43 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Well, in C or C++ it does something but likely not what the OP intended: the expression evaluates to (fully parenthesized)
((((character != 1) || 's') || 't') || 'd')
and just because all of 's' and 't' and 'd' evaluate to a non-zero value, the entire expression will be true; by sheer serendipity that is ;-)
kind regards,
Jos
- 12-22-2009, 02:14 PM #9
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Help please, stuck on for statement problem.
By shroomiin in forum New To JavaReplies: 3Last Post: 10-30-2009, 02:27 PM -
drawing char by char with Graphics
By diggitydoggz in forum New To JavaReplies: 5Last Post: 12-27-2008, 01:49 PM -
If-Else statement problem
By MomenT in forum New To JavaReplies: 9Last Post: 10-23-2008, 09:06 AM -
ForLoop statement problem
By MomenT in forum New To JavaReplies: 4Last Post: 10-01-2008, 02:01 PM -
Please help me with this Problem Statement
By KMS in forum New To JavaReplies: 1Last Post: 09-22-2008, 09:44 PM
Bookmarks