需要帮助在java中循环我的try和catch语句

时间:2013-11-26 02:24:42

标签: java loops while-loop try-catch

我称这种方法,它在大多数情况下都很有效。不确定这是否足以让你们从我们的问题中推断并弄清楚我的问题,但我想我会试一试......

当我输入一个超出数组范围的整数或文件名不存在时,它会抛出catch语句。我希望它然后循环回到程序要求的问题,而不仅仅是继续执行程序的其余部分。当我在与try语句相同的while循环中抛出catch语句时,我一直收到错误。感谢您的帮助,我希望这些内容足以让您了解。

public static String [][] placeCustomer(String [][] MovieSeats, int rows, int columns, String database)
{
    //Get user data and then write the name to the array space specified by the user..
    Scanner input = new Scanner(System.in);

    try 
        {
            File readFile = new File(database);
            Scanner reader = new Scanner(readFile);

            while (reader.hasNextLine())
            {
                String user = reader.nextLine();
                System.out.println(user + " wants to sit in the theater. Where would you like to place him?");
                String lastUser = user;

                System.out.print("Row: ");
                int placeRow = input.nextInt();

                System.out.print("Column: ");
                int placeCol = input.nextInt();


                while (!MovieSeats[placeRow][placeCol].equals("Seat Empty |")) //If element in 2-D array reads empty, then tell user.
                {
                    System.out.println("Sorry that seat is already taken.. try a different location.."); //Give them another chance to change location
                    System.out.println("Please enter a new location for " + user);
                    System.out.print("Row: ");

                    placeRow = input.nextInt();

                    System.out.print("Column: ");
                    placeCol = input.nextInt();

                    if (MovieSeats[placeRow][placeCol].equals("Seat Empty |")) //If it is empty, allow user to fill the 2-D element..
                    {
                        break;
                    }

                }

                if (MovieSeats[placeRow][placeCol].equals("Seat Empty |"))
                {


                    while (MovieSeats[placeRow][placeCol].equals("Seat Empty |")) 
                    {
                        System.out.println("The customer " + user + " has been placed at row " + placeRow + " and the column " + placeCol + ".");
                        System.out.println();


                        MovieSeats[placeRow][placeCol] = user;

                        System.out.println("The current seating \n________________________");

                        viewFilledTheater(MovieSeats, rows, columns);

                        System.out.println();
                    }
                }

                else 
                {
                    System.out.println("Please enter a valid value for the program to understand where you would like to place the customer...");   
                }


            }
        }



            //If the file does not exist, then catch the exception, print this statement and exit the program..

            catch (FileNotFoundException e) 
            {
                System.out.println("The movie theater will remain empty because \nwe cannot find the customer list with the name you provided..");

            }

            catch (ArrayIndexOutOfBoundsException e)
            {
                System.out.println("I am sorry, but the integer you entered is not within the proper bounds of the theater..");
            }







        return MovieSeats;

3 个答案:

答案 0 :(得分:1)

同时,我详细了解您的代码,我认为您可以让它变得更简单。您希望捕获ArrayIndexOutOfBoundsException,然后终端重新请求客户端输入placeRow,placeCol,因此,您应该将ArrayIndexOutOfBoundsException catch子句放在while循环中,同时将FileNotFoundException catch子句放在外面while循环。

下面是一个简单的演示,介绍如何使用ArrayIndexOutOfBoundsException try-catch子句来满足你的需要

while(true){
    System.out.println(user
    + " wants to sit in the theater. Where would you like to place him?");
    String lastUser = user;
    System.out.print("Row: ");
    int placeRow = input.nextInt();

    System.out.print("Column: ");
    int placeCol = input.nextInt();
    try{
        if(!MovieSeats[placeRow][placeCol].equals("Seat Empty |")){
            System.out.println("Sorry that seat is already taken.. try a different location..");
            System.out.println("Please enter a new location for "+ user);
            continue;
            }else{
            //set this seat occupied
            break;
        }

        }catch(ArrayIndexOutOfBoundsException e){
        //e.printStackTrace();
        continue;
    }

}

答案 1 :(得分:0)

所以,首先,你抛出异常,它们被catch语句捕获(这就是为什么他们称之为catch语句)。你的问题实际上只是范围界定之一。将try / catch嵌套在相关循环中。请注意,在异常之后,程序会在catch块之后重新开始。如果需要,您可以使用多个嵌套的try / catch语句。

答案 2 :(得分:0)

您应该构建一个执行此操作的递归方法:

步骤1.检查座位是否可用。如果座位可用,则放置用户并显示。

步骤2.如果座位不可用,请让用户查看用户是否喜欢重新输入他们的选择。如果是,请转到步骤1.如果否,请退出。

这样,无论用户选择了多少次错误的值,他总是可以选择重新输入。在用户选择之前,您的程序将永远不会退出。

我希望这会给你一些想法。祝你好运。