Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoSuchElement Exception

Tags:

java

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");
        }
    }
}
like image 494
James B Avatar asked Dec 11 '15 15:12

James B


People also ask

What is no such element exception?

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.

How do you solve no such element exception?

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.

What is exception in thread main Java Util NoSuchElementException?

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.

How do you handle no such element exception in selenium using try catch?

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.


3 Answers

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...

like image 69
brso05 Avatar answered Oct 19 '22 14:10

brso05


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.

like image 1
Mohammed Aouf Zouag Avatar answered Oct 19 '22 15:10

Mohammed Aouf Zouag


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.

like image 1
TangledUpInBlue Avatar answered Oct 19 '22 15:10

TangledUpInBlue