“线程中的异常”;“主要”;来自I/O误用的java.util.NoSuchElementException?
我写这个程序是为了作为飞机的座位表。我对在Java中使用I/O相当陌生,我遇到了以下运行时错误:“线程中的异常”;“主要”;来自I/O误用的java.util.NoSuchElementException?,java,io,nosuchelementexception,Java,Io,Nosuchelementexception,我写这个程序是为了作为飞机的座位表。我对在Java中使用I/O相当陌生,我遇到了以下运行时错误: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java: 862) at java.util.Scanner.next(Scanner.java: 1371) at AirplaneSeatProgram.main(AirplaneSeatPro
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java: 862)
at java.util.Scanner.next(Scanner.java: 1371)
at AirplaneSeatProgram.main(AirplaneSeatProgram.java:27)
我认为这与过早关闭I/O有关(特别是在代码的第27行?),但我不确定。有人能帮我吗?我将在下面发布代码
import java.util.*; //imports all java.util classes
import java.io.*; //importa all java.io classes
public class AirplaneSeatProgram{
//main method
public static void main(String[] args){
//constructs a Scanner object to accept input
Scanner myScanner=new Scanner(System.in);
//creates an input stream object linked to the file
Scanner inputStream;
char[][] mySeatMap=new char[9][8];
//tries to access the file
try{
inputStream=new Scanner(new FileInputStream("seatsmap.txt"));
for (int i=0; i<mySeatMap.length; i++){
for (int j=0; j<mySeatMap[i].length; j++){
//mySeatMap[i][j]=inputStream.next().charAt(0);
mySeatMap[i][j]=inputStream.next().charAt(0);
}
}
}
//if the file doesn't exist, create a file
catch (FileNotFoundException e) {
for (int i=0; i<mySeatMap.length; i++){
for (int j=0; j<mySeatMap[i].length; j++){
mySeatMap[i][j]='.';
}
}
}
//displays a menu for the user to select an operation until the user selects "Exit"
int myChoice=0;
while (myChoice!=3){
System.out.println();
System.out.println("Please select an operation:");
System.out.println("1: Reserve a seat");
System.out.println("2: Free a seat");
System.out.println("3: Exit");
myChoice=myScanner.nextInt();
display(mySeatMap);
//if the user selects 1, call the reserve() method
if (myChoice==1){
//checks to see if the input is valid
String regex="[0-9][a-hA-H]";
System.out.println("Enter a seat number (ex. 4C):");
String seatNumber=myScanner.next();
boolean isValid=isValid(seatNumber, regex);
int row=row(seatNumber);
int column=column(seatNumber);
System.out.println("row="+row);
System.out.println("column="+column);
//if the input is valid and the seat is available, reserve the seat
if (isValid){
if (mySeatMap[row][column]=='.'){
reserve(mySeatMap, row, column);
}
else{
System.out.println("Seat already taken.");
}
}
//otherwise throw an error
else{
InvalidSeatException e=new InvalidSeatException();
e.toString();
}
}
//if the user selects 2, call the free() method
if (myChoice==2){
//checks to see if the input is valid
String regex="[0-9][a-hA-H]";
System.out.println("Enter a seat number (ex. 4C):");
String seatNumber=myScanner.next();
boolean isValid=isValid(seatNumber, regex);
int row=row(seatNumber);
int column=column(seatNumber);
//if the input is valid and the seat is unavailable, free the seat
if (isValid){
if (mySeatMap[row][column]!='.'){
free(mySeatMap, row, column);
}
else{
System.out.println("Seat already free.");
}
}
//otherwise throw an error
else{
InvalidSeatException e=new InvalidSeatException();
e.toString();
}
}
if (myChoice==3){
//creates an output stream object linked to a file
PrintWriter outputStream;
try{
outputStream=new PrintWriter(new FileOutputStream("seatsmap.txt"));
for (int i=0; i<mySeatMap.length; i++){
for (int j=0; j<mySeatMap[i].length; j++){
outputStream.print(mySeatMap[i][j]+" ");
}
outputStream.println();
}
}
catch (FileNotFoundException e) {
System.out.println("Cannot create file because the file cannot be found.");
System.exit(0);
}
}
//if the user enters an invalid number from the menu, tell them to try again
if (myChoice>3){
System.out.println();
System.out.println("Invalid selection. Please try again.");
}
}
}
//method to reserve a seat
public static void reserve(char[][] a, int r, int c){
a[r][c]='x';
System.out.println("Successfully reserved your seat.");
}
//method to free a seat
public static void free(char[][] a, int r, int c){
a[r][c]='.';
System.out.println("Successfully freed the seat.");
}
//method to determine if seat is valid
public static boolean isValid(String a, String b){
if (!a.matches(b)){
return false;
}
else{
return true;
}
}
//method to return the row
public static int row(String a){
char b=a.charAt(0);
int c=Character.getNumericValue(b);
return c;
}
//method to return the column
public static int column(String a){
char b=a.charAt(0);
int c=(Character.getNumericValue(b))-2;
if(c>10){
int d=(Character.getNumericValue(b))-2;
return d;
}
else{
return c;
}
}
//method to display the seating chart
public static void display(char[][] a){
System.out.println();
System.out.println(" A B C D E F G H");
for (int i=0; i<9; i++){
System.out.print(i+1);
for (int j=0; j<8; j++){
System.out.print(" "+a[i][j]);
}
System.out.println();
}
}
}
import java.util.*//导入所有java.util类
导入java.io.*//导入所有java.io类
公共级飞机飞行计划{
//主要方法
公共静态void main(字符串[]args){
//构造扫描仪对象以接受输入
Scanner myScanner=新扫描仪(System.in);
//创建链接到文件的输入流对象
扫描仪输入流;
char[][]mySeatMap=new char[9][8];
//尝试访问该文件
试一试{
inputStream=新扫描仪(新文件inputStream(“seatsmap.txt”);
对于(int i=0;iwell,文件中可能没有足够的字符?行27
在哪里?能否提供seatsmap.txt
的内容?请在此处选中null inputStream.next()@Hannah不,您正在从磁盘读取一个文件。该文件的内容是什么?异常NoSuchElementException
告诉您,您正在尝试从流中读取下一个令牌,但没有剩余令牌。您的文件是空的,或者没有足够的令牌来读取…@SmartCoder Scanner.next()在任何情况下都不会返回null。