Results 1 to 6 of 6
  1. #1
    TheNadz is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Angry Scanner user input not working in conjunction with while loop.

    I'm sure I'm doing something completely idiotic but this is stumping me. I'm having the same issue with another program and have no clue where to go from here.

    Java Code:
    Scanner s = new Scanner(System.in);
    System.out.print(string1);
    while(s.nextInt() < 10)
    System.out.print(string2);
    int n = s.nextInt();
    When I comment out the while loop it works perfectly. With the while loop active, if I enter a number that shouldn't activate it, string2 is not printed but I'm given a second Scanner input box that shouldn't be there. If I do trigger the while loop, it prints string2 but then the same problem - it gives me a second, unnecessary Scanner input box.

    Can anyone help me? Please!

    Thanks!
    -Nadz
    Last edited by TheNadz; 11-27-2010 at 02:45 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    What is the purpose of this code? Can you show a small compilable program that demonstrates your problem? It preferably will have a small main method and nothing else.

    edit: and what do you mean by "scanner input box"?
    Last edited by Fubarable; 11-27-2010 at 02:46 AM.

  3. #3
    TheNadz is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Can do. Give me a second to get one together.

    EDIT: Here you go:

    Java Code:
    import java.util.*;
    
    public class test
    {
      
      public static void main(String[] args)
      {
        
        Scanner s = new Scanner(System.in);
        
        System.out.print("What n would you like to go up to?");
        while(s.nextInt() < 10)
          System.out.print("How about we try a bigger number, eh?");
        int n = s.nextInt();
        
        System.out.print(n);
        
      }
      
    }
    Last edited by TheNadz; 11-27-2010 at 02:48 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I would use a do-while loop since you have to run the loop at least once. Also, you must take care not to discard a desirable result as you do in your code above. If the user enters a number > 10, you get the number here:
    Java Code:
    while(s.nextInt() < 10)
    but you don't save it. That's why the user is forced to enter another number.

    e.g.,
    Java Code:
          Scanner s = new Scanner(System.in);
          int testInt = 0; 
          do {
             System.out.print("Enter an number greater than 9: ");
             testInt = s.nextInt();
          } while (testInt < 10);
          System.out.println("you entered: " + testInt);
    Also, you will want to enclose your while loop or do-while loop (or any loop or block) in curly braces. It may not seem important, but it will prevent some dumb mistakes (speaking from experience).

  5. #5
    TheNadz is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Thanks! I'll give that a go.

    And yeah, I tend to be lazy with the one line loops but it would be wise to use the brackets.

    -Nadz

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by TheNadz View Post
    Thanks! I'll give that a go.
    Great. Please let us know how it works out.


    And yeah, I tend to be lazy with the one line loops but it would be wise to use the brackets.
    and I used to be the same way, but no more. I find that using curly braces always makes for less work later, and so is a smarter way to be lazy.

Similar Threads

  1. while loop bypasses scanner input on 2nd pass
    By xf021209 in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 08:10 AM
  2. Scanner Not working
    By Xystus777 in forum New To Java
    Replies: 5
    Last Post: 03-18-2009, 12:22 AM
  3. [SOLVED] User Input - loop
    By new person in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 10:02 PM
  4. loop when there is no user-input
    By becky in forum New To Java
    Replies: 12
    Last Post: 02-02-2009, 10:02 PM
  5. Scanner Issues (User Input: Very Simple)
    By carlodelmundo in forum New To Java
    Replies: 8
    Last Post: 10-31-2008, 02:44 AM

Tags for this Thread

Posting Permissions

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