Can someone please help explain why this is throwing a NoSuchElement
exception? It seems to happen at the last line scan.nextInt();
I am trying to read in names from a file and sort them into an array and then read in an option from the user after printing.
import java.util.ArrayList;
import java.util.Collections;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class DeletingNames {
public static void main(String[] args)throws Exception {
int addDelete = 0;
int index = 0;
String addName;
String deleteName;
File namesFile = new File("names.txt");
ArrayList<String> names = new ArrayList<String>();
try {
Scanner scan = new Scanner(namesFile);
while(scan.hasNext()){
names.add(scan.next());
index++;
}
Collections.sort(names);
System.out.println(names);
System.out.println();
System.out.print("Add/delete data?\n1. Add\n2. Delete");
addDelete = scan.nextInt();
scan.close();
}
catch (FileNotFoundException e){
System.err.println("File not found");
}
}
}
The NoSuchElementException is an unchecked exception in Java that can be thrown by various accessor methods to indicate that the element being requested does not exist. Since the NoSuchElementException is thrown at runtime, it does not need to be declared in the throws clause of a method or constructor.
Solution. The solution to this exception is to check whether the next position of an iterable is filled or empty. You should only move to this position if the check returns that the position is not empty.
The exception in thread “main” java. util. nosuchelementexception error appears due to the lack of certain elements and functions in an enumeration. This is a standard error that many web developers face during their programming process with Java.
i.e. We have to use try .. catch blocks to handle the exception and also 'NoSuchElementException' WebDriver Exception Class needs to be used in the catch block as shown in the below code: 2. Hover the mouse over the 'NoSuchElementException' error in the above image and select 'import NoSuchElementException org.
Your Scanner
is still referencing the file
not System.in
:
scan.close();
scan = new Scanner(System.in);
addDelete = scan.nextInt();
You already went through the whole file so there are no more elements. That is why you are getting NoSuchElement
exception...
From the Javadoc:
A NoSuchElementException is Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration.
The execution flow won't leave this loop
while(scan.hasNext()){
names.add(scan.next());
index++;
}
....
// 'scan' here is invalid, create a new System.in scanner
System.out.print("Add/delete data?\n1. Add\n2. Delete");
addDelete = scan.nextInt(); // Invalid call
code block until there are no more elements to read from the text file. So calling scan.nextInt()
would throw a NoSuchElementException
. You need a new Scanner
to read the user's input.
Your while loop goes through everything in the scanner (scan.hasNext();), so there's nothing left when you come to "addDelete = scan.nextInt();"
If you want to take input from the user, you need to create a new scanner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With