0

I keep getting an error message from the output

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.nextDouble(Unknown Source)
    at test1.TC1.main(TC1.java:11)

but at the top it doesn't have any errors

package test1;

import java.util.Scanner;

public class TC1 {

    public static void main(String[] args){
        double name, age, answer;
        Scanner IN = new Scanner (System.in);
        System.out.println("Whats yor name?");
        name = IN.nextDouble();
        System.out.println("How old are you?");
        age = IN.nextDouble();
        answer = age + name;
        System.out.println(answer);
    }
}
0

3 Answers 3

1

name is actually a variable of type double. But maybe you are asking for the name which should be of type string.

Change the type of name and use name = IN.nextLine();

Sign up to request clarification or add additional context in comments.

3 Comments

i also have another problem that his guy named 19 isn't really helping me since i'm a begginer at this java thing...
it would be really helpful
0

Try this:

Change type of you variables to String instead of double, and use Scanner.next()

public class TC1 {

    public static void main(String[] args){
        String name, age, answer;
        Scanner IN = new Scanner (System.in);
        System.out.println("Whats yor name?");
        name = IN.next();
        System.out.println("How old are you?");
        age = IN.next();
        answer = age + name;
        System.out.println(answer);
    }
}

Comments

0

This exception will be thrown when you will give non-numeric input.

In your case you have declared name as a double, and I am quite sure that you are entering a non-numeric value in it, say Travis or Lita whatever :)

So either declare name as String and use IN.next() or try entering a numeric value :)

Comments

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.