1

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)
3
  • Use next instead of nextInt and add try/catch to handle NumberFormatException ... (Obviously use if to check if it's 1 or 2) Commented Sep 25, 2017 at 22:50
  • @Idos thanks! why would I use an if statement? it doesn't loop like a while loop. I don't want it to stop asking the user to enter in 1 or 2. Commented Sep 25, 2017 at 22:52
  • I meant use an if inside the loop. Commented Sep 25, 2017 at 22:52

3 Answers 3

2

Use nextLine instead of nextInt, and deal with the exception afterwards.

boolean accepted = false;
do {
    try {
        listChoice = Integer.parseInt(scan.nextLine());
        if(listChoice > 3 || listChoice < 1) {
            System.out.println("Choice must be between 1 and 2!");
        } else {
            accepted = false;
        }
    } catch (NumberFormatException e) {
        System.out.println("Please enter a valid number!");
    }
} while(!accepted);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank u so much!!! I think that is a really great thought process. I want to think logically too, but I am only a beginner and I feel stupid. I believe accepted = true in the else statement. If I put it as false, it keeps looping over and over again for me. And I also believe that it is if(listChoice >=3 || listChoice < 1)
It also still gives me trouble when I enter in a letter. I changed it to catch (InputMismatchException e) but it still says I have an InputMismatchException error.
2

You could try this with the Switch Case statement as well.

switch(listchoice)
{
    case 1:
    //Statment
    break;

    case 2:
    //statement
    break;

    default:
    System.out.println("Your error message");
    break;
}

Comments

1

Because the user can enter anything, you have to read in a line as a String:

String input = scan.nextLine();

Once you've done that, the test is easy:

input.matches("[12]")

All together:

String input = null;
do {
    System.out.println("Enter either 1 or 2:");
    input = scan.nextLine();
} while (!input.matches("[12]")); 

int listChoice = Integer.parseInt(input); // input is only either "1" or "2"

1 Comment

that's an interesting approach! i'll try it as well! thank u!

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.