简体   繁体   中英

Trouble finding data from text file

I've been having trouble extracting the data from a text file and using it. I've got an assignment that requires me to get 10 doubles from the file and find the min, max, and average of the numbers. This is what I've got so far.

 import java.util.*; import java.io.IOException; import java.util.Scanner; import java.io.File; public class DataAnalysis { static double i; public static void main(String args[]) { double sum =0; Scanner inputFile = new Scanner("input.txt"); double min = inputFile.nextDouble(); double max = inputFile.nextDouble(); for(i = inputFile.nextDouble(); i < 10; i++) { if(i < min) { min = i; } else { if(i > max) { max = i; } } } double average = sum/ 10; System.out.println("Maximum: " + max); System.out.println("Minimum: " + min); System.out.println("Average: " + average); } } 

It compiles just fine, but I get a Scanner InputMismatchException

 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 DataAnalysis.main(DataAnalysis.java:20) 

Any help with this would be appreciated!

It might be locale dependent. Decimal numbers are eg written as 0,5 in Sweden.

Change your code so that it says eg:

Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.US);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM