java - Scanner InputMismatchException from file -
doing first steps java, here's problem:
1.) rawdata.txt
can read:
import java.util.scanner; import java.io.file; import java.io.filenotfoundexception; import static java.lang.system.out; class readandwrite { public static void main(string args[]) throws filenotfoundexception { scanner diskscanner = new scanner(new file("rawdata.txt")); string diskstring; diskstring = diskscanner.nextline(); out.println(diskstring); diskscanner.close(); } }
the result in eclipse console :
>19.5 5
so guess content can read.
but: nextdouble()
, nextint()
won't work:
import java.util.scanner; import java.io.file; import java.io.filenotfoundexception; class readandwrite { public static void main(string args[]) throws filenotfoundexception { scanner diskscanner = new scanner(new file("rawdata.txt")); double unitprice, quantity; unitprice = diskscanner.nextdouble(); quantity = diskscanner.nextint(); [...] diskscanner.close(); } }
the error message console is:
exception in thread "main" java.util.inputmismatchexception @ java.util.scanner.throwfor(scanner.java:864) @ java.util.scanner.next(scanner.java:1485) @ java.util.scanner.nextdouble(scanner.java:2413) @ readandwrite.main(readandwrite.java:16)
so can understand problem?
i have made test on machine , i've found problem. expected double
format depends on locale
. in countries decimal digits separated ,
, .
.
scanner diskscanner = new scanner(new file("rawdata.txt")).uselocale(locale.english);
will solve problem. otherwise change file from
19.5 5
to
19,5 5
Comments
Post a Comment