0

I need to scan a number and then sort it in a list. How can i make the program ignore if a user types in letters instead of a number. it throws

Exception in thread "main" java.util.InputMismatchException.

code:

public static void InsertionSort() {

        LinkedList<Integer> list = new LinkedList<>();
        Scanner scan = new Scanner(System.in);
        
        int x=-2;
        while(x!=-1)
        {   
            System.out.println("type a number to place in a sorted list");
            x=scan.nextInt();
        }
        scan.close();
    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
meytal
  • 1
  • 3

0 Answers0