使用文本文件获取用户输入

Get input from user using a text file

在请求用户输入时,我无法使用 try catch 使我的代码正常工作。这是给定的作业:

BUS PASSENGER SEAT PROGRAM Write a program to assign passengers seats in a Bus. Assume a small bus with seat numbering as follows:
1 A B C D 2 A B C D 3 A B C D 4 A B C D 5 A B C D 6 A B C D 7 A B C D 8 A B C D The program should display the seat pattern, with an indicator (e.g. X) marking the seats already assigned. For example, after seats 1A, 2B and 4C are taken, the display should look like this: 1 X B C D 2 A X C D 3 A B C D 4 A B X D 5 A B C D 6 A B C D 7 A B C D 8 A B C D
After displaying the seats available, the program prompts for the set desired, the user types in a seat, and then the display of available seats is updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that the seat is occupied and ask for another choice. The program outputs the ticket the user booked in a text file and in UI with the following details: : The program is also capable of accepting an input text file that contains the details above and determines if the seat number content on the text file is already occupied or not. Likewise, the summary of reserved seats together with the passenger name can be viewed in UI and in another text file. Sorry for the unorganized placing, I cant seem to put new lines in it.

到目前为止,这是我的代码:

package busticket;

import java.util.*;
import java.io.*;

public class BusTicket {

    public static void printArray(char[][] Ticket) {
        for (int i = 0; i < Ticket.length; i++) {
            System.out.println((i + 1) + "   " +
                    Ticket[i][0] + " " + Ticket[i][1] + "  " +
                    Ticket[i][2] + " " + Ticket[i][3]);
        }
    }

    public static void main(String[] args) {
        Scanner get = new Scanner(System.in);
        PrintWriter a;
        String f;

        char Again;
        char[][] Ticket = new char[8][4];


        System.out.println("Hello Sir/Ma'am here is the list of the available seats:");

        for (int i = 0; i < 8; i++) {
            Ticket[i][0] = 'A';
            Ticket[i][1] = 'B';
            Ticket[i][2] = 'C';
            Ticket[i][3] = 'D';
        }

        printArray(Ticket);

        int counter = 0;
        do {

            do {
                System.out.println("Press 1 for Manual input.");
                System.out.println("Press 2 for Text input.");
                int Choice = get.nextInt();

                if (Choice == 1) {
                    get.nextLine();
                    System.out.println("Please enter your name: ");
                    String name = get.nextLine();
                    System.out.println("Please enter your desired seat. (ex: 1A).");
                    String seat = get.nextLine();

                    int row = seat.charAt(0) - '1';
                    int column = seat.charAt(1) - 'A';

                    if (row < 0 || row > 8 || column < 0 || column > 4) {
                        System.out.println("Wrong input.");
                    } else {
                        if (Ticket[row][column] != 'X') {
                            Ticket[row][column] = 'X';
                            printArray(Ticket);
                            System.out.println("Reserved:" + name + "-" + seat);

                            try {
                                a = new PrintWriter(new FileWriter("output.txt", true));
                                a.write(String.format(name));
                                a.write("-");
                                a.write(String.format(seat));
                                a.write("\n");
                                a.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            counter++;
                        } else {
                            System.out.println("THAT SEAT IS TAKEN!!!");

                        }
                    }
                } else if (Choice == 2) {
                    get.nextLine();
                    try {
                        String str;
                        Scanner g = new Scanner(new File("input.txt"));
                        while ((str = g.nextLine()) != null) {
                            String[] ar = str.split("-");
                            String name = ar[0];
                            String seat = ar[1];
                        }
                    } catch (Exception e) {
                    }

                    int row = seat.charAt(0) - '1';
                    int column = seat.charAt(1) - 'A';

                    if (row < 0 || row > 8 || column < 0 || column > 6) {
                        System.out.println("Wrong input.");
                    } else {
                        if (Ticket[row][column] != 'X') {
                            Ticket[row][column] = 'X';
                            printArray(Ticket);
                            System.out.println("Reserved:" + name + "-" + seat);

                            try {
                                a = new PrintWriter(new FileWriter("output.txt", true));
                                a.write(String.format(name));
                                a.write("-");
                                a.write(String.format(seat));
                                a.write("\n");
                                a.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            counter++;
                        } else {
                            System.out.println("THAT SEAT IS TAKEN!!!");

                        }
                    }
                }
                do {
                    System.out.print("\nReserve again? Press Y to Reserve again. Press N to exit and print the receipt.(Y/N): ");
                    Again = get.next().charAt(0);
                    Again = Character.toUpperCase(Again);
                    get.nextLine();
                } while ((Again != 'Y') && (Again != 'N'));
            } while (Again == 'Y');
            if (Again == 'N' || Again == 'n') {
                try {
                    Scanner d = new Scanner(new File("output.txt"));
                    while ((f = d.nextLine()) != null) {
                        System.out.println("Reserved:");
                        System.out.println(f);

                    }
                } catch (Exception e) {
                }
                System.exit(0);
            }
        } while (counter < 32);
    }
}

我的问题是第二个选择。 这部分:

else if (Choice==2){
         get.nextLine();
            try {
            String str;
            Scanner g = new Scanner (new File("input.txt"));
            while ((str = g.nextLine()) != null){
            String[] ar=str.split("-");
            String name=ar[0];
            String seat=ar[1];
            }
            }catch (Exception e){}

如何让我的代码在不破坏我的代码逻辑的情况下读取 String 名称和 try catch 中的位置? try catch里面的所有变量都读取不到。

All variables inside the try catch can't be read

我猜你的意思是"I cannot reference the variables that I declare inside the try-catch from outside the try-catch."

如果这是你的问题,请在该块之外声明它们,如下所示:

String name = null;
String seat = null;

try
{
  // logic to determine name
  name = ar[0];
  seat = ar[1];
}
catch
{
  e.printStackTrace(); // always put something in the catch
}

然后你就可以引用那个块外的变量了。变量范围在声明变量的块内;您可以在内部范围内使用该变量,但不能在外部范围内使用。