My program should accept file input of any data type and display it. However after reading the 7th element, I get the error "NoSuchElementException"
This is my code:
My program should accept file input of any data type and display it. However after reading the 7th element, I get the error "NoSuchElementException"
This is my code:
In the while loop you are doing two "in.next()" in a row without checking "in.hasNext()"
You should store the in.next() in a variable and then add that variable to ArrayType and LinkType.
while(in.hasNext()) {
Object o = in.next();
ArrayType.add(o);
LinkType.add(o);
}
Based on your comment, if you just want to print it out to see that everything else is working, use this:
public static void main(String[] args) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(("input.txt")));
for (String line : lines) {
System.out.println(line);
}
}
I think you are trying to do this:
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(new FileReader("input.text"));
List<String> arrayType = new ArrayList<>(100);
List<String> linkedType = new LinkedList<>();
while (in.hasNext()){
String line = in.next();
arrayType.add(line);
linkedType.add(line);
}
System.out.println(arrayType);
}