线程"main"中的异常java.util.NoSuchElementException:找不到行 [英] Exception in thread "main" java.util.NoSuchElementException: No line found

查看:86
本文介绍了线程"main"中的异常java.util.NoSuchElementException:找不到行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取用户输入,并且在输入第二个值之后出现此错误:

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

线程"main"中的异常java.util.NoSuchElementException:找不到行在java.util.Scanner.nextLine(未知来源)在Reservations.start(Reservations.java:50)处在Reservations.main(Reservations.java:29)

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("-------------------------\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();
        }
}

推荐答案

您应该处理此异常,或者只使用

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

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

这篇关于线程"main"中的异常java.util.NoSuchElementException:找不到行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆