Results 1 to 12 of 12
  1. #1
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default Scanner.next() within an if statement.

    I have this code:
    Java Code:
    inputVerb = keyboard.next().toLowerCase();
                if (!inputVerb.equals("look")) {
                    inputObject1 = keyboard.next().toLowerCase();
                }
                if (!inputVerb.equals("spawn") || !inputVerb.equals("remove") || !inputVerb.equals("look")) {
                    withCheck = keyboard.next().toLowerCase();
                    inputObject2 = keyboard.next().toLowerCase();
                }
    As you can see, when the user inputs "look" as the first keyboard.next(), I don't want the programme to look for any more words for the input, so I inserted those ifs. When I run the programme, it still looks for those extra 3 next()s when I type "look".
    Last edited by ngc0202; 08-11-2010 at 08:09 PM.

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

    Default

    I think you have to change your control flow structure to this:

    Java Code:
    if ( ... ) {
    }
    else if ( ... ) {
    }
    kind regards,

    Jos

  3. #3
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    I didn't really want it to do anything but move right over the stuff in the ifs if the statement was false, so an else wouldnt really be very useful.

    EDIT: still waiting for replies!
    Last edited by ngc0202; 08-11-2010 at 09:22 PM.
    Support the cause on 10/10/10!
    http://ngc0202.webs.com/thegame.htm

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    move right over the stuff in the ifs if the statement was false
    Does the inverse apply: Do the 'stuff in the if' if the if statement is true?
    Then put the second if inside the first if.
    If the first if is false, the the code inside that if is "moved over".

    Normally this kind of logic is handled as JosAH suggested.
    When an if is true, all the rest of the else if tests are skipped.

  5. #5
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    I don't want an else statement though, I want it to do nothing if the statement in the if is false, which is not what it's doing. It appears to be doing the stuff in the if statement when I am sure that the statement in the if is false.:confused:
    Support the cause on 10/10/10!
    http://ngc0202.webs.com/thegame.htm

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    I want it to do nothing if the statement in the if is false
    How would that look in pseudo code?
    Java Code:
    if(condition) {
      //1) execute here if the condition is true
    }else{
      //2) execute here if the condition is false
    }
    If everything is in the //1) area and nothing is in the //2) area, then "nothing will be done" if the if condition is false.
    So put all the code in the //1) area

  7. #7
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    I am fully aware of how to write if statements. I posted the code I used in my first post, check it out. I'm saying that's exactly what I wrote, yet it is still executing the stuff it shouldn't be during those conditions.
    Support the cause on 10/10/10!
    http://ngc0202.webs.com/thegame.htm

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    I am fully aware of how to write if statements
    We were wondering about that.

    it is still executing the stuff it shouldn't be
    Why is it executing that stuff? What conditions control whether the stuff is executed?
    Use a println() inside the if() block to show the values of ALL the conditions being tested in the if. That should show you which one is allowing the if block to execute. For example:
    System.out.println(!inputVerb.equals("spawn") + " " + !inputVerb.equals("remove") + " " + !inputVerb.equals("look"));

  9. #9
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    if (!inputVerb.equals("spawn") || !inputVerb.equals("remove") || !inputVerb.equals("look")) {
    withCheck = keyboard.next().toLowerCase();
    inputObject2 = keyboard.next().toLowerCase();
    }

    erm, it seems to me this if statment is always true. You used, or, so if one of those statements is true, the whole statement is true. The first part is false when you typ "spawn", but then inputverb can't be "remove", so "!inputVerb.equals("remove") " is true and then the whole statement is true. So that seems to be the problem.

  10. #10
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    Oh, I see, so I should have made it && as opposed to ||. That was probably a dumb mistake :P
    Support the cause on 10/10/10!
    http://ngc0202.webs.com/thegame.htm

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    The OP would see that if he added the println() I suggested be added to the code to show the condition values.

  12. #12
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    Yes, I'm sorry Norm, I was a little distracted by IRC, then Imorios post popped up in the corner and reminded me all about it. Thank you also.
    Support the cause on 10/10/10!
    http://ngc0202.webs.com/thegame.htm

Similar Threads

  1. Using scanner for CSV
    By getName() in forum Advanced Java
    Replies: 7
    Last Post: 06-20-2010, 04:33 PM
  2. Need help with scanner.
    By mainy in forum New To Java
    Replies: 3
    Last Post: 07-28-2009, 02:11 PM
  3. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 03:37 PM
  4. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:00 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 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
  •