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)