views:

25

answers:

2

I'm trying to read a long type from a text file using Scanner in Java. I get the following error:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextLong(Scanner.java:2196)
at java.util.Scanner.nextLong(Scanner.java:2156)
at Project.main(Project.java:119)

Which correlates to this line:

strLine = (long) in.nextLong();

If I do in.next() it will work, but I need to store the info as a long, not as a String. The exact number it's getting mad at reading is: 3.20e11

Anyone know how to fix this? Thanks in advance!

+1  A: 

It thinks 3.20e11 is a double, which is why you get the input mismatch. Try the input as 320000000000 and it will work.

dcp
Ahh, didn't realize that was the issue. Thanks!
watley8
+1  A: 

3.20e11's double.

You should do:

strLine = (long) in.nextDouble();

oneat
Ahh, didn't realize that was the issue. Thanks!
watley8