0

I want the user to input integers between 80 and 120 with no alphabets and other symbols. Here are my codes:

import java.util.*;

public class Test {     

public static void main(String[] args)
{       
    Scanner in = new Scanner(System.in);

//checking for integer input
    while (!in.hasNextInt())
    {
        System.out.println("Please enter integers between 80 and 120.");
        in.nextInt();
        int userInput = in.nextInt();

//checking if it's within desired range
        while (userInput<80 || userInput>120)
        {
            System.out.println("Please enter integers between 80 and 120.");
            in.nextInt();
        }
    }
}

}

However, I'm facing an error. Is there a solution to this?

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Array.main(Array.java:15)

Thank you! :)

EDIT: Thank you Tom, got the solution, but would like to try without "do"

    Scanner in = new Scanner(System.in);

    int userInput;
    do {
    System.out.println("Please enter integers between 80 and 120.");
    while (!in.hasNextInt())
    {
        System.out.println("That's not an integer!");
        in.next();
        }
        userInput = in.nextInt();
} while (userInput<81 || userInput >121);

System.out.println("Thank you, you have entered: " + userInput);
}
}
Ken
  • 93
  • 1
  • 8
  • Sure there is! `catch` the `java.util.InputMismatchException` and deal with it appropriately. Out of interest, why are you skipping over an input? – Bathsheba Apr 26 '16 at 12:30
  • @Bathsheba hi there, i would like to do it without use of catch, as I'm revising my school work and this is just the first few topics (haven't learnt catch exception at this point) – Ken Apr 26 '16 at 12:35
  • Change the condition to while (in.hasNextInt()) – Unknown Apr 26 '16 at 12:37

2 Answers2

0

Your loop condition is wrong. You check something like "as long as there is no integer available from the input: read an integer". That is gonna fail

Also: You are calling nextInt twice. Don't do that. Remove the first call:

System.out.println("Please enter integers between 80 and 120.");
in.nextInt(); //REMOVE THIS LINE
int userInput = in.nextInt();

You are checking once if an int is available using hasNextInt, but then you read a value twice!

f1sh
  • 9,331
  • 3
  • 22
  • 47
0
    boolean continueOuter = true;

    while (continueOuter)
        {
            System.out.println("Please enter integers between 80 and 120.");
            String InputVal = in.next();

        try {
            int input = Integer.parseInt(InputVal);
              //checking if it's within desired range
             if(FirstInput>80 && FirstInput<120)
            {
               //no need to continue got the output
                continueOuter = false;
            }else{
                  System.out.println("Please enter integers between 80 and  120."); //need to continue didnt got the exact output
                 continueOuter = true;      
            } 

        } catch (Exception e) {
        //not an int    
       System.out.println(InputVal);
       continueOuter = true;
        } 
}

in this code i have created a boolean to check whether the program wants to continue or not. if the user has entered a valid value the program will stop however you can change that as you wish. and you dont need two while loops i have changed the inner while loop to a if loop take a look at my code

Priyamal
  • 2,747
  • 1
  • 17
  • 44