Question

[Solved] Exception in thread “main” java.util.NoSuchElementException: No line found [duplicate]

I’m trying to get user input and after the 2nd input value I get this error:

Exception in thread “main” java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Reservations.start(Reservations.java:50)
at Reservations.main(Reservations.java:29)

import java.util.Scanner; // Needed to read user input

public class Reservations {

        // Boolean array for seating [false = available, true = taken]
        static boolean[][] seats;

        // Main method
        public static void main(String[] args) {

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

                        // Welcome message
                        System.out.println("-------------------------");
                        System.out.println("Welcome to NSCC AirLines.");
                        System.out.println("-------------------------
");

                        // Starts program
                        start();
                }
        }

        public static void start() {

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

                // Variables for user input
                String requestedSection;
                String requestedSeat;

                // Counters for seating array
                int countSection = 0;
                int countSeat = 0;

                // Prompts the user to select their choice of section
                System.out.print("Please type 1 for First Class or 2 for Economy: ");

                // Section preference
                requestedSection = sc.nextLine();

                switch (requestedSection) {
                case "1":
                        // User selects first class
                        System.out.println(">>> You have selected First Class. 
");
                        break;

                case "2":
                        // User selects economy
                        System.out.println(">>> You have selected Economy. 
");
                        break;

                default:
                        // User has not selected a valid class
                        System.out
                                        .println(">>> You have not selected a valid class. Please try again. 
");
                        start();
                        break;
                }

                // Prompts the user to select their choice of seat
                System.out.print("Please type 1 for window and 2 for aisle: ");

                // Seat preference
                requestedSeat = sc.nextLine();

                switch (requestedSeat) {
                case "1":
                        // User selects first class
                        System.out.println(">>> You have selected a window seat. 
");
                        break;

                case "2":
                        // User selects economy
                        System.out.println(">>> You have selected an aisle seat. 
");
                        break;

                default:
                        // User has not selected a valid class
                        System.out.println(">>> You have not selected a valid seat. Please try again. 
");
                        start();
                        break;
                }

                // Closes Scanner
                sc.close();
        }
}
Enquirer: Jonny

||

Solution #1:

The API document of readLine() method says as below

Throws:
    NoSuchElementException - if no line was found 

You are supposed to handle this exception or just use the hasNextLine() method to avoid the exception.

while(sc.hasNextLine()){
    requestedSeat = sc.nextLine();
}
Respondent: Jonny

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy