Hello all, I've been fooling around with some code trying to teach myself somewhat about Linked Lists. I wrote this quick program that creates a random number in each node of a list and asks the user to guess the number. I have a quit choice that, when executed alone (no prior menu choice used) works perfectly. If the user, though, were to choose option 1 or 2 before attempting to quit, the program would throw an InpurMismatchException. I'm just curious as to why this is happening.

I'm not sure what's triggering the error. My guess would be something with my

keyboard.nextInt()

statements, but that's just a wild guess.

Here's the code:

package linkedtest;

import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;

public class LinkedTest {

    public static int getMenuChoice() {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("\nEnter a menu choice: "
                + "\n1) Generate randomized list"
                + "\n2) Guess number"
                + "\n3) Quit");

        return keyboard.nextInt();
    }
    public static void main(String[] args) {
        LinkedList<Integer> test = new LinkedList<Integer>();
        
        Random generator = new Random();
        Scanner keyboard = new Scanner(System.in);
        
        int randQuant = 0;
        int guess = 0;
        String askQuit;
        
        int menuChoice;
        menuChoice = getMenuChoice();
        boolean areWeDone = false;
        
        while (!areWeDone) {
            if (menuChoice == 3) {
                System.out.println("\nAre you sure you want to quit? (Y/N)");
                askQuit = keyboard.nextLine().toUpperCase();

                if (askQuit.equals("Y")) {
                    areWeDone = true;
                    break;
                }
            }

            switch (menuChoice) {
                case 1:
                    
                    System.out.println("Enter range for random number "
                            + "generation: ");
                    randQuant = keyboard.nextInt();

                    for( int i = 0; i < 30; i++){
                        int rand = generator.nextInt(randQuant);
                        test.add(rand);
                    }
                    
                    System.out.println("List as been generated.");
                    
                    break;
                    
                case 2:
        
                    System.out.println("Guess a number from 0 to " + randQuant);
                    guess = keyboard.nextInt();
                    
                    if(test.contains(guess)){
                        System.out.println(guess + " is in the list.");
                        test.remove(guess);
                    }
                    
                    else {
                        System.out.println("Number not in the list, try again.");
                    }
                    
                    break;
                    
            }
            
            menuChoice = getMenuChoice();
    }
  }
}

and the error:

Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:909)
	at java.util.Scanner.next(Scanner.java:1530)
	at java.util.Scanner.nextInt(Scanner.java:2160)
	at java.util.Scanner.nextInt(Scanner.java:2119)
	at linkedtest.LinkedTest.getMenuChoice(LinkedTest.java:18)
	at linkedtest.LinkedTest.main(LinkedTest.java:79)
Java Result: 1

Thanks!

Hello all, I've been fooling around with some code trying to teach myself somewhat about Linked Lists. I wrote this quick program that creates a random number in each node of a list and asks the user to guess the number. I have a quit choice that, when executed alone (no prior menu choice used) works perfectly. If the user, though, were to choose option 1 or 2 before attempting to quit, the program would throw an InpurMismatchException. I'm just curious as to why this is happening.

I'm not sure what's triggering the error. My guess would be something with my

keyboard.nextInt()

statements, but that's just a wild guess.

Here's the code:

package linkedtest;

import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;

public class LinkedTest {

    public static int getMenuChoice() {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("\nEnter a menu choice: "
                + "\n1) Generate randomized list"
                + "\n2) Guess number"
                + "\n3) Quit");

        return keyboard.nextInt();
    }
    public static void main(String[] args) {
        LinkedList<Integer> test = new LinkedList<Integer>();
        
        Random generator = new Random();
        Scanner keyboard = new Scanner(System.in);
        
        int randQuant = 0;
        int guess = 0;
        String askQuit;
        
        int menuChoice;
        menuChoice = getMenuChoice();
        boolean areWeDone = false;
        
        while (!areWeDone) {
            if (menuChoice == 3) {
                System.out.println("\nAre you sure you want to quit? (Y/N)");
                askQuit = keyboard.nextLine().toUpperCase();

                if (askQuit.equals("Y")) {
                    areWeDone = true;
                    break;
                }
            }

            switch (menuChoice) {
                case 1:
                    
                    System.out.println("Enter range for random number "
                            + "generation: ");
                    randQuant = keyboard.nextInt();

                    for( int i = 0; i < 30; i++){
                        int rand = generator.nextInt(randQuant);
                        test.add(rand);
                    }
                    
                    System.out.println("List as been generated.");
                    
                    break;
                    
                case 2:
        
                    System.out.println("Guess a number from 0 to " + randQuant);
                    guess = keyboard.nextInt();
                    
                    if(test.contains(guess)){
                        System.out.println(guess + " is in the list.");
                        test.remove(guess);
                    }
                    
                    else {
                        System.out.println("Number not in the list, try again.");
                    }
                    
                    break;
                    
            }
            
            menuChoice = getMenuChoice();
    }
  }
}

and the error:

Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:909)
	at java.util.Scanner.next(Scanner.java:1530)
	at java.util.Scanner.nextInt(Scanner.java:2160)
	at java.util.Scanner.nextInt(Scanner.java:2119)
	at linkedtest.LinkedTest.getMenuChoice(LinkedTest.java:18)
	at linkedtest.LinkedTest.main(LinkedTest.java:79)
Java Result: 1

Thanks!

The problem is here:

public static int getMenuChoice() {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("\nEnter a menu choice: "
                + "\n1) Generate randomized list"
                + "\n2) Guess number"
                + "\n3) Quit");

        return keyboard.nextInt();
    }

when the user enters 'Y' to quit it tries to read an integer but of course it recieves a character/string thats whats throwing the exception....use nextLine() method and then check if its 'y' or 'n' then work appropriately if its neither convert to a integer... any exception thrown by the conversion is an actual input mismatch
[edit] This would mean that you'd have to take away all other checks for y or n and make it all coincide with getMenuChoice. Also use equalsIgnoreCase() method

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.