我写了这个程序,作为飞机的座位表。我在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(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();
}
}
}