I need to write a program that reads and saves the entered file in Java in a double array. The number of values in the file is stored in the first line of the file, then the actual data values follow.
Here is what I still have:
public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); System.out.print("Please enter the name of the input file: "); String inputFileName = console.next(); Scanner in = new Scanner(inputFileName); int n = in.nextInt(); double[] array = new double[n]; for( int i = 0; i < array.length; i++) { array[i] = in.nextDouble(); } console.close(); }
The input file is as follows:
ten
43628.45
36584.94
76583.47
36585.34
86736.45
46382.50
34853.02
46378.43
34759.42
37,658.32
At the moment, regardless of the file name I entered, I get an error message:
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.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at Project6.main(Project.java:33)
source share