I am running into an error within my program when trying to get input from user. Basically what the program is, it is to pull data from two different exported txt files which I will then cross reference from each of them. At this point I am just trying to test out the file information gathering that the program is going to do prior to cross referencing.
What is happening, I read the one file which is "FromFile.txt" and then it will print out everything and then ask what the other file name that we need to test. At this point it will then throw that error the 2nd time it asks for the input from the user.
I am having difficulties trying to figure out why this is throwing the error
This is the error that is being thrown.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at CrossReferencingTool.getInputString(CrossReferencingTool.java:98)
at CrossReferencingTool.main(CrossReferencingTool.java:23)
Below is the code where it is having the error.
public static String getInputString() {
Scanner input = new Scanner(System.in);
String string;
string = input.next(); <----------------- This is the line where it is throwing the error
input.close();
return string;
}
below is the overall code that is being ran.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class CrossReferencingTool {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<ArrayList<String>> list1 = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> list2 = new ArrayList<ArrayList<String>>();
System.out.println("What is the filename of the file you want to cross reference from:");
File file1 = new File(getInputString());
populateList(file1, list1);
printList(list1);
System.out.println("\nwhat is the filename of the file you want to cross reference with:");
File file2 = new File(getInputString());
populateList(file2, list2);
printList(list2);
}
private static void printList(ArrayList<ArrayList<String>> list) {
String row;
for(int x = 0; x < list.size(); x++) {
row = "";
for(int y = 0; y < list.get(x).size(); y++) {
row += list.get(x).get(y);
if( (y + 1) < (list.get(x).size())) {
row += ", ";
}
}
System.out.println(row);
}
}
private static void populateList(File file, ArrayList<ArrayList<String>> list) throws FileNotFoundException {
Scanner reader = new Scanner(file);
String splitLine[];
String line;
ArrayList<String> internalList;
while(reader.hasNextLine()) {
internalList = new ArrayList<String>();
line = reader.nextLine();
splitLine = line.split(",");
for(int x = 0; x < splitLine.length; x++) {
internalList.add(splitLine[x]);
}
list.add(internalList);
internalList = null;
}
reader.close();
}
public static String getInputString() {
Scanner input = new Scanner(System.in);
String string;
string = input.next();
input.close();
return string;
}
}