Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Quick handling of the exception in thread "main" java.util.nosuchelementexception: no line found error
Next
Answered
Atlas Rodriguez
  • 29
Atlas Rodriguez
Asked: May 18, 20222022-05-18T06:52:54+00:00 2022-05-18T06:52:54+00:00In: java

Quick handling of the exception in thread “main” java.util.nosuchelementexception: no line found error

  • 29

. Advertisement .

..3..

. Advertisement .

..4..

I am new to java and searching the “exception in thread “main” java.util.nosuchelementexception: no line found” to understand it better. It seems it doesn’t work as expected when I used some suggestions before. Here is the command line I use:

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("-------------------------\n");
 
  // 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. \n");
  break;
 
  case "2":
  // User selects economy
  System.out.println(">>> You have selected Economy. \n");
  break;
 
  default:
  // User has not selected a valid class
  System.out
  .println(">>> You have not selected a valid class. Please try again. \n");
  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. \n");
  break;
 
  case "2":
  // User selects economy
  System.out.println(">>> You have selected an aisle seat. \n");
  break;
 
  default:
  // User has not selected a valid class
  System.out.println(">>> You have not selected a valid seat. Please try again. \n");
  start();
  break;
  }
 
  // Closes Scanner
  sc.close();
  }
 }

The error I’m getting is below:

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)

Please give me the solution to this issue.

no line found
  • 1 1 Answer
  • 60 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    hdtutoria Expert
    2022-06-05T03:42:39+00:00Added an answer on June 5, 2022 at 3:42 am

    The cause: Various coding flaws will cause this exception message to appear. The most typical cause of this error is when programmers call nextLine() in the context of a Scanner and there is no next line to follow.

    The solution: The readLine() method’s API documentation states the following:

    Throws: 
        NoSuchElementException - if no line was found

    You can either handle the exception or just use the hasNextLine() function to avoid it.

    while(sc.hasNextLine()){
        requestedSeat = sc.nextLine();
    }
    • 12
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.