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

时间:2021-04-19 23:59:14

I couldn't get my code work using the try catch when asking for the user's input. Here's the given homework:

在询问用户的输入时,我无法使用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.

巴士乘客座位计划编写一个程序,在巴士中分配乘客座位。假设一个小型公共汽车的座位编号如下:1 A B C D 2 A B C D 3 A B C D 6 A B C D 7 A B C D 8 A B C D程序应显示座位图案,并带有标记已分配座位的指示器(例如X)。例如,在取下座椅1A,2B和4C后,显示器应如下所示:1 XBCD 2 AXCD 3 ABCD 4 ABXD 5 ABCD 6 ABCD 7 ABCD 8 ABCD显示可用座椅后,程序会提示输入所需的设置,用户键入座位,然后更新可用座位的显示。这一直持续到所有座位都被填满或者直到用户发出该程序应该结束的信号。如果用户键入已经分配的座位,则程序应该说座位已被占用并要求另一个选择。程序输出用户在文本文件和UI中预订的票证,其中包含以下详细信息::程序还能够接受包含上述详细信息的输入文本文件,并确定文本文件中的座位号内容是否已经是否被占用。同样,可以在UI和另一个文本文件中查看预订座位的摘要以及乘客姓名。对于无组织的放置感到抱歉,我似乎无法在其中添加新的线条。

Here's my code so far:

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

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);
    }
}

My problem is with the 2nd choice. This part:

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

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){}

How can I let my code read the String name and seat inside the try catch without destroying the logic of my code? All variables inside the try catch can't be read.

如何在不破坏代码逻辑的情况下让我的代码读取String名称并在try catch中放置?无法读取try catch中的所有变量。

1 个解决方案

#1


All variables inside the try catch can't be read

无法读取try catch中的所有变量

I'm going to guess that you mean "I cannot reference the variables that I declare inside the try-catch from outside the try-catch."

我猜你的意思是“我不能引用我在try-catch外面的try-catch中声明的变量。”

If that's your question, then declare them outside that block, like this:

如果这是你的问题,那么在那个区块之外声明它们,如下所示:

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
}

Then you can refer to the variables outside that block. Variable scope is inside the block in which the variable is declared; you can use the variable in inner scopes, but not in outer ones.

然后你可以引用那个块之外的变量。变量范围位于声明变量的块内;你可以在内部范围中使用变量,但不能在外部范围内使用。

#1


All variables inside the try catch can't be read

无法读取try catch中的所有变量

I'm going to guess that you mean "I cannot reference the variables that I declare inside the try-catch from outside the try-catch."

我猜你的意思是“我不能引用我在try-catch外面的try-catch中声明的变量。”

If that's your question, then declare them outside that block, like this:

如果这是你的问题,那么在那个区块之外声明它们,如下所示:

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
}

Then you can refer to the variables outside that block. Variable scope is inside the block in which the variable is declared; you can use the variable in inner scopes, but not in outer ones.

然后你可以引用那个块之外的变量。变量范围位于声明变量的块内;你可以在内部范围中使用变量,但不能在外部范围内使用。