I'm having so much trouble with this. I have tried multiple things but it still gives me an error message and crashes. I want to loop it until the user enters 1 or 2.
System.out.println("Please choose if you want a singly-linked list or a doubly-linked list.");
System.out.println("1. Singly-Linked List");
System.out.println("2. Doubly-Linked List");
int listChoice = scan.nextInt();
//Restrict user input to an integer only; this is a test of the do while loop but it is not working :(
do {
System.out.println( "Enter either 1 or 2:" );
listChoice = scan.nextInt();
} while ( !scan.hasNextInt() );
It gives me this error:
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 Main.main(Main.java:35)
nextinstead ofnextIntand addtry/catchto handleNumberFormatException... (Obviously useifto check if it's 1 or 2)ifinside the loop.