java do while asked for input twice

Viewed 753

why do I need to input 2 times? please help I'm new to java, can't find the error

do {
    System.out.println("Enter your age: ");
    age= in.nextInt();
    if(!in.hasNextInt()) {
        System.out.println("Please enter a valid age");
        in.nextInt();
        valid=false;
    }
}while(valid);

I removed the do while but it still asks for the second input

System.out.println("Enter your age: ");
age= in.nextInt();
if(!in.hasNextInt()) { //Will run till an integer input is found
    System.out.println("Please enter a valid age");
    in.nextInt();
    valid=false;
}
4 Answers

I have updated your code. This will work for you.

public class Example
{
    public static void main(String[] args) {
      boolean valid = true;  
      do {
          Scanner in = new Scanner(System.in);
          System.out.println("Enter your age: ");
          while(!in.hasNextInt()) {
             System.out.println("Please enter a valid age");
             in.next();
             valid = false;
          }
          int age = in.nextInt();
      } while(valid);
    }     
}

Output :

Enter your age:                                                                                                                                                          
2                                                                                                                                                                        
Enter your age:                                                                                                                                                          
seven                                                                                                                                                                   
Please enter a valid age                                                                                                                                                 
seven                                                                                                                                                                   
Please enter a valid age                                                                                                                                                 
3 

Explanation : If you are giving valid data, then the loop will continue to take inputs (not taking inputs twice). As soon as, you give invalid data, then, the code will prompt you to enter the valid data and loop will stop executing as you made valid = false.

The reason why it asks you for input twice is due to you in.hasNextInt() which will check your scanner for input from the system. Since your system does not have any input due to you calling age = in.nextInt(); which will move the scanner to the next word before your in.hasNextInt(), The function in.hasNextInt() will require you to input something so that it can validate if it is an Int or not.

what we want to do, is to first check the current scanner's input if it has an integer before we either store it inside age or loop again and ask for new input.

A better way of checking would be to do something like this.

import java.util.*;
class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int age = 0;
    System.out.println("Enter your age: ");
    while(!in.hasNextInt()){// checks if scanner's next input is an int, return true if next input is not an Int and the while loop continues till the next input is an Int
      System.out.println("Please enter a valid age: ");
      in.nextLine();//move the scanner to receive the next nextLine
      //this is important so the hasNextInt() wont keep checking the same thing
    }
    //it will only exit the while loop when user have successfully enter an interger for the first word they inputted.
    age = in.nextInt();
    System.out.println("Your age is: " + age);
  }
}

Output:

Enter your age: 
boy
Please enter a valid age: 
boy girl
Please enter a valid age: 
5
Your age is: 5

You should move the System.out.println("Enter your age: "); statement outside the do-while loop.

Hi : D there is becuase of the !in.hasNextInt() it will cause you need to do the input again but you can change it to other condition like if the age is bigger than certain value.

public class stackTest {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int age =0 ;
        boolean valid = false;
        do {
            System.out.println("Enter your age: ");
            age= in.nextInt();
            if(age>90) {
                System.out.println("Please enter a valid age");
                valid=true;
            }
            else valid=false;
        }while(valid);
        System.out.println("Age: " + age);
        
          
    }
}
Related