Exception in Thread Main Java.Util.NoSuchElementException: No Line Found

Sheeraz Gul Jan 30, 2023 Jul 27, 2022
Exception in Thread Main Java.Util.NoSuchElementException: No Line Found

This tutorial demonstrates solving the Exception in thread "main" java.util.NoSuchElementException: No line found in Java.

Exception in thread "main" java.util.NoSuchElementException: No line found

The java.util.NoSuchElementException is a runtime unchecked exception. The JVM raises this exception when we use methods like next(), nextElement(), Iterators, or methods or enumerations.

The error Exception in thread "main" java.util.NoSuchElementException: No line found occurs when we are using a Scanner to get user input with methods like nextLine(); the error will occur when we are trying to use the method without any boundary.

Let’s try an example that demonstrates this error.

package delftstack;

import java.util.Scanner;

public class Example {


    static boolean[][] Articles;
    public static void main(String[] args) {

        // This initiates all array values to be false
        Articles = new boolean[4][4];
        for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    Articles[i][j] = false;
                }

               // Welcome message
                System.out.println("-------------------------");
                System.out.println("Welcome to Delftstack.com.");
                System.out.println("-------------------------\n");

               // Starts program
               Programstart();
        }
    }

    public static void Programstart() {

      // to read users' input
       Scanner sc = new Scanner(System.in);

       //user input
        String Requested_Lanuguage;
        String Requested_Article;

        // Counters for articles array
        int Count_Language = 0;
        int Count_Artciles = 0;

        // User to select their choice of Programming Language
        System.out.print("Please type 1 for Java or 2 for Python: ");

        // Language preference
        Requested_Lanuguage = sc.nextLine();

        switch (Requested_Lanuguage) {
            case "1":
                // User selects Java
                System.out.println(">>> You have selected Java. \n");
                break;

            case "2":
                // User selects Python
                System.out.println(">>> You have selected Python. \n");
                break;

            default:
                // User has not selected a valid Programming Language
                System.out.println(">>> You have not selected a valid choice. Please try again. \n");
                Programstart();
                break;
        }

       // user to select their choice of article
       System.out.print("Please type 1 for Web and 2 for App: ");

       // Article preference
       Requested_Article = sc.nextLine();

       switch (Requested_Article) {
           case "1":
               // User selects Web Articles
               System.out.println(">>> You have selected Web Articles. \n");
               break;

           case "2":
               // User selects App Articles
               System.out.println(">>> You have selected App Articles. \n");
               break;

           default:
                // User has not selected a valid article
                System.out.println(">>> You have not selected a choice. Please try again. \n");
                Programstart();
                break;
            }

        // Closes Scanner
       sc.close();
    }
}

The output for the code above is:

-------------------------
Welcome to Delftstack.com.
-------------------------

Please type 1 for Java or 2 for Python: 1
>>> You have selected Java.

Please type 1 for Web and 2 for App: 1
>>> You have selected Web Articles.

Exception in thread "main" -------------------------
Welcome to Delftstack.com.
-------------------------

Please type 1 for Java or 2 for Python: java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at delftstack.Example.Programstart(Example.java:45)
    at delftstack.Example.main(Example.java:24)

The error occurs because we use the method nextLine() without any boundary. To solve this issue we need to replace the code Requested_Article = sc.nextLine(); to the following code.

while(sc.hasNextLine()){
    Requested_Article = sc.nextLine();
    // Switch condition here
}

Let’s try the solution.

package delftstack;

import java.util.Scanner;

public class Example {

    static boolean[][] Articles;
    public static void main(String[] args) {

        // This initiates all array values to be false
        Articles = new boolean[4][4];
        for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    Articles[i][j] = false;
                }

               // Welcome message
                System.out.println("-------------------------");
                System.out.println("Welcome to Delftstack.com.");
                System.out.println("-------------------------\n");

               // Starts program
               Programstart();
        }
    }

    public static void Programstart() {

        // to read users' input
        Scanner sc = new Scanner(System.in);

        //user input
        String Requested_Lanuguage;
        String Requested_Article;

        // Counters for articles array
        int Count_Language = 0;
        int Count_Artciles = 0;

        // User to select their choice of Programming Language
        System.out.print("Please type 1 for Java or 2 for Python: ");

        // Language preference
        Requested_Lanuguage = sc.nextLine();

        switch (Requested_Lanuguage) {
            case "1":
                // User selects Java
                System.out.println(">>> You have selected Java. \n");
                break;

            case "2":
                // User selects Python
                System.out.println(">>> You have selected Python. \n");
                break;

            default:
                // User has not selected a valid Programming Language
                System.out.println(">>> You have not selected a valid choice. Please try again. \n");
                Programstart();
                break;
        }

       // user to select their choice of article
       System.out.print("Please type 1 for Web and 2 for App: ");

       // Article preference
       while(sc.hasNextLine()){
            Requested_Article = sc.nextLine();

       switch (Requested_Article) {
           case "1":
               // User selects Web Articles
               System.out.println(">>> You have selected Web Articles. \n");
               break;

           case "2":
               // User selects App Articles
               System.out.println(">>> You have selected App Articles. \n");
               break;

           default:
                // User has not selected a valid article
                System.out.println(">>> You have not selected a choice. Please try again. \n");
                Programstart();
                break;
            }
        }
        // Closes Scanner
       sc.close();
    }
}

The code above will not throw the Exception in thread "main" java.util.NoSuchElementException: No line found now. See the output:

-------------------------
Welcome to Delftstack.com.
-------------------------

Please type 1 for Java or 2 for Python: 1
>>> You have selected Java.

Please type 1 for Web and 2 for App: 1
>>> You have selected Web Articles.

1
>>> You have selected Web Articles.


>>> You have not selected a choice. Please try again.

Please type 1 for Java or 2 for Python:
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Exception