I don't know why my code isn't working. It's worked before but I don't know why this time it isn't.
public static void inputData() throws IOException{ Scanner keyboard = new Scanner(System.in); String input; int lines; System.out.println("Please enter a file name: "); input = keyboard.nextLine(); File myfile = new File(input); if (!myfile.exists()){ System.out.println("File not found."); System.exit(0); } Scanner inputFile = new Scanner(myfile); lines = inputFile.nextInt(); System.out.println(lines); int[] array = new int[lines]; while (inputFile.hasNext()){ for (int i=0; i<=lines; i++){ int moreLines = inputFile.nextInt(); array[i] = moreLines; } } System.out.println(array[3]); inputFile.close(); }
Here's what the error returns:
Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:838) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextInt(Scanner.java:2091) at java.util.Scanner.nextInt(Scanner.java:2050) at Paniagua_ArrayProcessing.inputData(Paniagua_ArrayProcessing.java:24) at Paniagua_ArrayProcessing.main(Paniagua_ArrayProcessing.java:5)