Exception Exception Exception

Can someone explain why this throws a NoSuchElement exception? This seems to be happening on the last line of scan.nextInt();

I am trying to read in the names from a file and sort them in an array, and then read the option with the user after it.

 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"); } } } 
+5
source share
4 answers

Your Scanner still refers to file not System.in :

 scan.close(); scan = new Scanner(System.in); addDelete = scan.nextInt(); 

You have already viewed the entire file, so there are no more items. This is why you get the NoSuchElement exception ...

+3
source

From Javadoc :

A NoSuchElementException is thrown by the nextElement method of an enumeration that indicates what to enumerate.

The thread of execution will not 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 

until there are no more items to read from a text file. Therefore, a call to scan.nextInt() will raise a NoSuchElementException . To read user input, you will need a new Scanner .

+1
source

Your while loop goes through everything in the scanner (scan.hasNext ();), so there is nothing left when you come to "addDelete = scan.nextInt ();"

If you want to enter data from the user, you need to create a new scanner.

+1
source

NoSuchElementException will be thrown if there are no more tokens. This is caused by calling nextInt () without checking for an integer. This exception is thrown to indicate that there are no more elements in the enumeration. This exception extends the RuntimeException class and therefore applies to those exceptions that can be thrown while the Java Virtual Machine (JVM) is running. This is an unchecked exception and therefore does not need to be declared in the throwers constructors method or clause. Finally, NoSuchElementException exists since the first version of Java.

java.util.NoSuchElementException is a RuntimeException that can be thrown by various classes in Java, for example, Iterator, Enumerator, Scanner or StringTokenizer.

According to the Javadoc, a NoSuchElementException is raised by the NextElement method of an enumeration to indicate that there are no more elements in the enumeration.

To prevent this, you can use hasNext () to check if more tokens are available.

0
source

Source: https://habr.com/ru/post/1237931/


All Articles