1

I would like to use Scanner to scan any number of integer then get the average. Before I stop, I would like to type "END".

The code below has: Exception in thread "main" java.util.InputMismatchException error. That is because I scan a string rather than int type.

How should I solve this problem?

Thanks

public static int scanaverage()
{
System.out.println("Enter any number, type 'END' to exit");
Scanner input = new Scanner(System.in);
int total=0; 
int count = 0;

while (!(input.nextLine().equals("END")))
{
total += input.nextInt();
count += 1;
}
return total / count;
}
1
  • Use input.nextLine() to read the value as a String and then use Integer.parseInt() to convert String to int. use equalsIgnoreCase() check before parsing Commented Nov 30, 2015 at 13:20

3 Answers 3

2

You could just scan the whole line and check if the line is a number like this

public static int scanaverage() {
    System.out.println("Enter any number, type 'END' to exit");
    Scanner input = new Scanner(System.in);
    int total = 0;
    int count = 0;
    String line;
    do {
        line = input.nextLine();
        try {
            total += Integer.parseInt(line); // Cast the number, if it does not succeed catch the exception.
            count += 1;
        } catch(NumberFormatException e) {
            if(!line.equalsIgnoreCase("end")) { // Wrong input
                System.out.println("Wrong input, input another number or end");
            }
        }
    } while (!line.equalsIgnoreCase("end"));
    return total / count;
}
Sign up to request clarification or add additional context in comments.

Comments

1

The Exception is due to two read operation for getting one number, For Example take input as

1

2

3

END

Debug:

while loop condition input.nextLine() will fetch 1 then input.nextInt() will fetch 2 while loop condition input.nextLine() will fetch 3 then input.nextInt() will fetch END --> This will throw InputMismatchException

Hope the bellow code will work, except the part that Any non int input will break the loop

public static int scanaverage()
{
System.out.println("Enter any number, type 'END' to exit");
Scanner input = new Scanner(System.in);
int total=0; 
int count = 0;

while (input.hasNextInt()))
{
total += input.nextInt();
count += 1;
}
return total / count;
}

1 Comment

Well, this doesn't support the - user should type "end" part
0

You can simply add a try catch block as follows.

public static int scanaverage()
{
  System.out.println("Enter any number, type 'END' to exit");
  Scanner input = new Scanner(System.in);
  int total=0; 
  int count = 0;

  while (!(input.nextLine().equals("END")))
  {
   try{
    total += input.nextInt();
    count += 1;
    }catch(InputMismatchException e){

    }
  }
  return total / count;
}

I haven't test it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.