Hello! This is my first time trying to use Java, and I was trying to learn something with the "Scanner userInput" and ".nextInt()". After I put a bit of code on, it showed me:

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 test.test.main(test.java:14)

This is the code:

package test;

import java.util.Scanner;

public class test {

public static void main(String[] args) {
    int name = 0;

    Scanner userInput;
    userInput = new Scanner(System.in);
    name = userInput.nextInt();

    System.out.println("Hello" + name + "!");

    }

}

Recommended Answers

All 2 Replies

Input Mismatch Excption happens because the nextInt expects an integer value but the input cannot be parsed as an int.
What exactly did you type for the input when you ran the program?

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.