1

Sorry for asking an easy question, but I'm a total beginner here. So the problem is when I copy the following code into my eclipse, it's working fine. But when I change the type of the variables from int to double it's showing some kind of an error. Please check it out.

import java.util.Scanner;

class AddNumbers
{
   public static void main(String args[])
   {
      double x, y, z;
      System.out.println("Enter two integers to calculate their sum ");
      Scanner in = new Scanner(System.in);
      x = in.nextDouble();
      y = in.nextDouble();
      z = x + y;
      System.out.println("Sum of entered integers = "+z);
   }
}

btw, the error is as following

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 AddNumbers.main(AddNumbers.java:10)

here's the video tutorial i just watched (https://www.youtube.com/watch?v=ANuuSFY2BbY) i just tried copying it

import java.util.Scanner;

class HelloWorld{

    public static void main(String args[]) {
        Scanner bucky = new Scanner(System.in);
        double fnum;
        double snum;
        double answer;
        System.out.println("Enter first number:");
        fnum = bucky.nextDouble();
        System.out.println("Enter second number:");
        snum = bucky.nextDouble();
        answer = fnum+snum;
        System.out.print(answer);
    }

}

and i get the following error:

Enter first number:
34.6
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 HelloWorld.main(HelloWorld.java:11)
Aba74
  • 55
  • 4

2 Answers2

1

Answer

You see. I get the right answer. So you have to show your inout like what they have said.

Also you can modify your code like below to make it more stronger.

import java.util.InputMismatchException;
import java.util.Scanner;

class AddNumbers
{
public static void main(String args[])
  {
    double x, y, z;
    System.out.println("Enter two double to calculate their sum ");
    Scanner in = new Scanner(System.in);

    while (true) {
        try {
            x = Double.parseDouble(in.nextLine());
            y = Double.parseDouble(in.nextLine());
            z = x + y;
            System.out.println("Sum of entered double = " + z);
            break;
        } catch (Exception e) {
            System.out.println("Not a double param, please enter again");
            continue;
        }
    }

}

}

Dai Kaixian
  • 1,045
  • 2
  • 14
  • 24
0

You probably entered something that CANNOT be interpreted as a double ( letters or special charcters ).

You strictly have to enter ONLY numbers.

Check your input.

  • I only entered 34.6. Please, if possible, also check out the following tutorial and inform me if it works https://www.youtube.com/watch?v=ANuuSFY2BbY – Aba74 Nov 19 '16 at 07:15
  • You have probably set a Locale that uses `,` instead of `.`. – Thilo Nov 21 '16 at 01:28