Results 1 to 9 of 9
  1. #1
    KardKaper is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default 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
    
    }
    What is wrong with this?

    Thanks for the help.

    Mike
    Last edited by KardKaper; 12-20-2009 at 07:24 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by KardKaper View Post
    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
    
    }
    What is wrong with this?

    Thanks for the help.

    Mike
    Test your boolean expression by hand: suppose character == 'X'; it most certainly isn't an 's' or 'd' or 't' so your boolean expression is true. You didn't have this in mind but what did you have in mind?

    kind regards,

    Jos

  3. #3
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    Also remember that 's' != 'S'... it's case sensitive.
    Please don't laugh at my English... I'm trying my best! :)

  4. #4
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    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}
    i come from c++ origins... so that's how i would do it... i think the syntax would work in java... i don't see why it wouldn't
    Last edited by helpisontheway; 12-21-2009 at 12:32 PM.

  5. #5
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    That doesn't work with Java!
    Please don't laugh at my English... I'm trying my best! :)

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by renamed View Post
    That doesn't work with Java!
    It doesn't work in C++ nor C either.

    kind regards,

    Jos

  7. #7
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    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! :)

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by renamed View Post
    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.
    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

  9. #9
    ProGenius is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by KardKaper View Post
    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
    
    }
    What is wrong with this?

    Thanks for the help.

    Mike
    Try with a SWITCH statement. It may be a bit longer but at least you will be able to control what happenes with all of the alphabet letters. After you get itworking, try to change it to if else. That way you know what works and what doesnt. Thats
    how I would do it

Similar Threads

  1. Help please, stuck on for statement problem.
    By shroomiin in forum New To Java
    Replies: 3
    Last Post: 10-30-2009, 01:27 PM
  2. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 PM
  3. If-Else statement problem
    By MomenT in forum New To Java
    Replies: 9
    Last Post: 10-23-2008, 08:06 AM
  4. ForLoop statement problem
    By MomenT in forum New To Java
    Replies: 4
    Last Post: 10-01-2008, 01:01 PM
  5. Please help me with this Problem Statement
    By KMS in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 08:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •