watley8
watley8

Reputation: 3

Having issues reading Long types from a textfile using Java scanner

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!

Upvotes: 0

Views: 1469

Answers (2)

oneat
oneat

Reputation: 10994

3.20e11's double.

You should do:

strLine = (long) in.nextDouble();

Upvotes: 3

dcp
dcp

Reputation: 55458

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

Upvotes: 1

Related Questions