[英]Why would this simple code throw "NoSuchElementException"?
[英]Why does my code throw a NoSuchElementException?
我学校作业的Java程序给我一个错误:
Welcome to Surinam Airlines - We may get you there!
We have 6 seats available for flight JAVA123.
How many seats do you want to book? 3
Enter passenger #1's name: Taylor Swift
Enter passenger #1's food option
(V = vegetarian, N = no preference, H = Halal): V
Enter passenger #1's class (1 = first, 2 = business, 3 = economy): 1
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown source)
at Passenger.enterPassenger(Passenger.java:64)
at RunAirLine.main(RunAirLine.java:23)
尽管我不太清楚为什么,但似乎与未实例化该对象有关。 有人知道怎么了吗?
import java.util.*;
public class RunAirLine {
private static Passenger seatArray[][] = new Passenger[2][3]; // declares new
// array of
// Passengers
static Scanner kb = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Welcome to Surinam Airlines – We may get you there!");
System.out.println();
int seats = 6; // available seats
while (seats > 0) {
System.out.println("We have " + seats
+ " seats available for flight JAVA123.");
System.out.println();
System.out.print("How many seats do you want to book? ");
int n = kb.nextInt();
System.out.println();
for (int i = 0; i < n; i++) {
int x = emptySeat()[0];
int y = emptySeat()[1];
// retrieves info from emptySeat() function
seatArray[x][y] = new Passenger();
seatArray[x][y].enterPassenger(7 - seats); // gives the method the nth
// passenger number
seats--; // takes away 1 from the available seats
}
System.out.println();
}
System.out.println("No seats available.");
System.out.println();
System.out.println("Flight manifest:");
System.out.println();
for (int y = 0; y < 3; y++) { // prints out the list of flyers
for (int x = 0; x < 2; x++) {
System.out.println(seatArray[x][y].getSeat() + " "
+ seatArray[x][y].printPassengerCode());
}
}
kb.close();
}
public static int[] emptySeat() {
for (int y = 0; y < 3; y++) {// finds the next empty seat
for (int x = 0; x < 2; x++) {
if (seatArray[x][y] == null || seatArray[x][y].getSeat().equals("")) {
// if the spot hasn't been declared yet or is empty
return new int[] { x, y };
}
}
}
return null; // if none found then return null
}
}
import java.util.*;
public class Passenger {
private String name;
private String seat;
private char foodOption;
private int seatClass;
// reduces some redundancy
private String classStr;
private String prefStr;
public Passenger() { // constructors
name = "";
seat = "";
foodOption = ' ';
seatClass = -1;
}
public Passenger(String n, String s, char f, int sc) { // assigns values
// according to input
name = n;
seat = s;
foodOption = f;
seatClass = sc;
}
// get and set methods
public void setName(String n) {
name = n;
}
public void setSeat(String s) {
seat = s;
}
public void setFoodOption(char f) {
foodOption = f;
}
public void setSeatClass(int sc) {
seatClass = sc;
}
public String getName() {
return name;
}
public String getSeat() {
return seat;
}
public char getFoodOption() {
return foodOption;
}
public int getSeatClass() {
return seatClass;
}
// end of get and set methods
public void enterPassenger(int i) {
Scanner kb = new Scanner(System.in);
// asks the important questions
System.out.print("Enter passenger #" + i + "’s name: ");
name = kb.nextLine();
System.out.println("Enter passenger #" + i + "’s food option ");
System.out.print("(V = vegetarian, N = no preference, H = Halal): ");
foodOption = kb.nextLine().toUpperCase().charAt(0);
System.out.print("Enter passenger #" + i
+ "’s class (1 = first, 2 = business, 3 = economy): ");
seatClass = kb.nextInt();
System.out.println();
char seatChar = 'A'; // calculates the seat number
if (i % 2 == 0)
seatChar = 'B';
seat = Integer.toString(i / 2 + i % 2) + seatChar;
classStr = "Economy Class"; // transfers the class choice int into string
switch (seatClass) {
case 1:
classStr = "First Class";
break;
case 2:
classStr = "Business Class";
break;
}
prefStr = "No preference"; // transfers the preference char into string
switch (foodOption) {
case 'V':
prefStr = "Vegetarian";
break;
case 'H':
prefStr = "Halal";
break;
}
System.out.println("Done – Seat " + seat + " booked");
System.out
.println("-------------------------------------------------------------------");
System.out.println(name + "(" + classStr + " - Seat " + seat + ") - "
+ prefStr);
System.out
.println("-------------------------------------------------------------------");
kb.close();
}
public void printTicketStub() {
char initial = name.split(" ")[0].toUpperCase().charAt(0); // splits name
// into first
// name and
// second name
System.out.println(initial + ". " + name.split(" ")[1] + "(" + classStr
+ " - Seat " + seat + ") - " + prefStr);
}
public String printPassengerCode() { // forms the code as required
return seatClass + name.toUpperCase().substring(name.length() - 4)
+ foodOption;
}
}
您有两个问题:
您为System.in
创建了一个Scanner
两次。 您只能执行一次。 我已注释掉您在Passenger.java
创建的那个,并将引用更改为RunAirLine.java
中的RunAirLine.java
:
public void enterPassenger(int i) { // Scanner kb = new Scanner(System.in); // asks the important questions System.out.print("Enter passenger #" + i + "'s name: "); name = RunAirLine.kb.nextLine(); System.out.println("Enter passenger #" + i + "'s food option "); System.out.print("(V = vegetarian, N = no preference, H = Halal): "); foodOption = RunAirLine.kb.nextLine().toUpperCase().charAt(0); System.out.print("Enter passenger #" + i + "'s class (1 = first, 2 = business, 3 = economy): "); seatClass = RunAirLine.kb.nextInt(); RunAirLine.kb.nextLine();
每次您执行nextInt()
,它都不会使Scanner的读数出现新的一行,这会弄乱东西。 如果要使nextInt()
在其自己的行上,则应抛出一个虚拟RunAirLine.kb.nextLine();
在nextInt()
调用之后。 这需要做两次,一次在我上面确定的代码中,一次在您的RunAirLine.class
:
System.out.print("How many seats do you want to book? "); int n = kb.nextInt(); kb.nextLine(); System.out.println();
进一步阅读为什么不应该使用多个Scanner
。 的tl; dr是关闭System.in
永远关闭流。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.