0

So, what I'm trying to do is I want to add together two numbers containing decimals. The code below somehow accepts and works when I use integers, but once I use double as the first input I receive an error message.

Here's the code:

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        Scanner kalkulator = new Scanner(System.in);

        double num1, num2, resultat;

        System.out.println("Enter first number: "); 
        num1 = kalkulator.nextDouble();

        System.out.println("Enter second number: "); 
        num2 = kalkulator.nextDouble();

        resultat = num1 + num2;
        System.out.println("Equals: " + resultat);
    }
}

Error saying:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextDouble(Scanner.java:2413)
    at Calculator.main(Calculator.java:11)

Why does it not accept double, and what do I have to change to make it work with double instead of integer?

-Tom

EJK
  • 12,332
  • 3
  • 38
  • 55

0 Answers0