Results 1 to 4 of 4
  1. #1
    tony1420 is offline Member
    Join Date
    Oct 2014
    Posts
    1
    Rep Power
    0

    Default nextInt in a while loop

    Hello, so i just wanted to restrict a user to use numbers in a range so i made a while loop but it gets completely ignored

    Java Code:
    num1 = input.nextInt();
    		while(num1 < 0 || num1 > 1000){
    			System.out.println("reEnter First Number:");
    			num1 = input.nextInt();
    		}
    int num1 is declared as an int so not sure why it is not working when entering a number less then or greater then 1000
    Last edited by tony1420; 10-05-2014 at 05:59 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: nextInt in a while loop

    I don't see where you are consuming the newline that is left in the Scanner buffer after reading the nextInt. Maybe that's the cause?

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates your problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: nextInt in a while loop

    Shouldn't be a problem.
    That "newline left in buffer" only causes trouble when mixing the nextXXX() methods with a call to next(), which (unless there's something missing from the example) doesn't appear to be the case here.

    I'll echo the SSCCE request, though.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Java Guy is offline Member
    Join Date
    Oct 2014
    Location
    Null
    Posts
    3
    Rep Power
    0

    Default Re: nextInt in a while loop

    I would have to see your code in its entirety to narrow down your error. I can see in the above code that you don't have num1 declared but did attempt to initialized it at runtime with user input. Your logic, minus the things mentioned tin the foregoing sentence seems to work fine. Here is my solution.
    Hope this helps.

    Scanner input = new Scanner(System.in);
    int num; //Declares the num variable to be an integer data type.
    num = input.nextInt();
    while(num < 0 || num > 1000)
    {
    System.out.println("Renter");
    num = input.nextInt();
    }

Similar Threads

  1. nextInt() repeat
    By trishtren in forum New To Java
    Replies: 3
    Last Post: 10-09-2011, 06:20 PM
  2. Replies: 1
    Last Post: 09-30-2011, 12:13 AM
  3. nextint() Method !
    By M7MD in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-25-2010, 06:13 AM
  4. Very Bad problem with nextInt()
    By usiyalla in forum New To Java
    Replies: 2
    Last Post: 07-25-2010, 01:44 AM
  5. nextInt(unknown source)
    By babyburger in forum New To Java
    Replies: 3
    Last Post: 03-18-2010, 04:03 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
  •