2

I was trying to get 10 Integer input from users. In addition, I want to handle the exception when users enter the wrong type of data (not Integer). However, I have this problem when using the for loop & try/catch together. For example, if I enter String at the 4th number. I would get this as a result:

Type 1. integer: 15
Type 2. integer: 152 
Type 3. integer: 992
Type 4. integer: jj
Invalid number
Type 5. integer: Invalid number
Type 6. integer: Invalid number
Type 7. integer: Invalid number
Type 8. integer: Invalid number
Type 9. integer: Invalid number
Type 10. integer: Invalid number

Integers: [15, 152, 992]

I don't know how to reenter the loop after the exception is caught.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    Integer integer;
    List<Integer> integerList = new ArrayList<Integer>();
    for (int i = 1; i < 11; i ++) {
        System.out.print("Type " + i + ". integer: ");
        try {
        integer = input.nextInt();
        integerList.add(integer);
        }
        catch (InputMismatchException exc) {
            System.out.println("Invalid number");
        }

    }
    System.out.println("Integers: " + integerList);
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Hieu Cao
  • 91
  • 5
  • What exactly is the problem with the output you posted? What is the question? – user207421 Oct 05 '16 at 23:38
  • 1
    First you need to create an MCVE: http://stackoverflow.com/help/mcve Then you need to reset the scanner buffer: http://stackoverflow.com/questions/10604125/how-can-i-clear-the-scanner-buffer-in-java Last, you need to reset 'i' so you can retry the same input. – Daniel Wisehart Oct 06 '16 at 00:20

3 Answers3

1

you don't leave the for-loop on exception. instead of a for-loop, i'd recommend a while-loop, e.g.

//  your code
while (integerList.size() < 10) {
    Scanner input = new Scanner(System.in);
    //  your code
    try {
    //  your code
    }
    catch (InputMismatchException exc) {
        input.nextLine();
        //  your code
    }
    //  your code
}
//  your code
mre
  • 43,520
  • 33
  • 120
  • 170
  • 1
    It still doesn't solve the problems for me. For some reasons, the exception statement is caught as an input for the next Integer. Using the while loop created an infinitive loop here. Is there anything wrong with my exception handling argument? – Hieu Cao Oct 05 '16 at 22:24
  • @HieuCao Add `input.nextLine()` to your catch block before the error message (see http://stackoverflow.com/a/25277332/584862) – mre Oct 05 '16 at 23:24
-1

Or decrease the counter when ever exception count so that cycle won't be count.

 catch (InputMismatchException exc) {
            System.out.println("Invalid number");
            i--;       
}
Ammad
  • 4,031
  • 12
  • 39
  • 62
-1

You have to clear the Scanner input:

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.InputMismatchException;

class foo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        Integer integer;
        List<Integer> integerList = new ArrayList<Integer>();
        for (int i = 1; i < 11; i ++) {
            System.out.print("Type " + i + ". integer: ");
            try {
                integer = input.nextInt();
                integerList.add(integer);
            }
            catch (InputMismatchException exc) {
                System.out.println("Invalid number");
                input.nextLine();
                --i;
            }
        }
        System.out.println("Integers: " + integerList);
    }
}
Daniel Wisehart
  • 1,489
  • 12
  • 31