Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do I fix this Java error?

import java.util.Scanner;

public class NameID {

public static void main(String[] args) {

Scanner questioner = new Scanner(System.in);

double name;

System.out.println("Hello stranger, what is your name?");

System.out.println("Enter your name: ");

name = questioner.nextDouble();

System.out.println("Welcome to Elderzia, " + name);

}

}

Here's the output:

Hello stranger, what is your name?

Enter your name:

Zalvager

Exception in thread "main" java.util.InputMismatchException

at java.util.Scanner.throwFor(Unknown Source)

at java.util.Scanner.next(Unknown Source)

2 Answers

Relevance
  • Anonymous
    9 years ago
    Favorite Answer

    import java.util.Scanner;

    public class NameID {

    public static void main(String[] args) {

    Scanner questioner = new Scanner(System.in);

    String name;

    System.out.println("Hello stranger, what is your name?");

    System.out.println("Enter your name: ");

    name = questioner.next();

    System.out.println("Welcome to Elderzia, " + name);

    fixed code above will work

    explanation: The reason is a double is a number value like 2.01111 you needed to use a String for the name and questioner.next (); will take in a String value.

  • 9 years ago

    Type in a number. You are tying to force a String into a double field.

    Or change the name variable to be a String.

    Also change the assignment to be

    name = questioner.next();

    Have fun.

Still have questions? Get your answers by asking now.