Home > Software design >  My condition for handling IndexOutOfBonds Exception doesn't work
My condition for handling IndexOutOfBonds Exception doesn't work

Time:12-10

I have a two dimensional array like a cinema and the user selects the seat he wants to buy , I am testing here the conditions that if the user enters the wrong seat coordination the program should output "Wrong Input!" but it doesn't work for some reason and I don't know why.

Here is my main

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int numberOfRows;
    int numberOfSeatsInEachRow;
    System.out.println("Enter the number of rows:");
    numberOfRows = scanner.nextInt();
    System.out.println("Enter the number of seats in each row:");
    numberOfSeatsInEachRow = scanner.nextInt();
    String [][] cinema = new String[numberOfRows][numberOfSeatsInEachRow   1];
    System.out.println();
    menu(cinema,numberOfSeatsInEachRow,numberOfRows,scanner);
}

Here is my function that does all the checkings , everything works except the "Wrong Input!" condition that I highlighted with comment "this condition"

public static void pickASeat(String[][] cinema, int numberOfRows, int numberOfSeatsInEachRow, Scanner scanner) {
    System.out.println();
    int rowNumber = 0;
    int seatNumber = 0;
    do {

            System.out.println("Enter a row number:");
             rowNumber = scanner.nextInt();
            System.out.println("Enter a seat number in that row:");
             seatNumber = scanner.nextInt();

        if (cinema[rowNumber - 1][seatNumber].equals("B")) {
            System.out.println("That ticket has already been purchased!");
          [**////this condition**]
        } else if (rowNumber > numberOfRows || seatNumber > numberOfSeatsInEachRow) {
            System.out.println("Wrong input!");

        } else {
            System.out.println();
            cinema[rowNumber - 1][seatNumber] = "B";
            int ticketPrice = 0;
            int totalNumberOfSeats = numberOfRows * numberOfSeatsInEachRow;
            int numberOfElementsInArray = 0;
            if (totalNumberOfSeats <= 60) {
                ticketPrice = 10;
                System.out.println("Ticket price: $"   ticketPrice);
            } else if(numberOfRows % 2 == 0) {
                if (rowNumber - 1 <= numberOfRows / 2 - 1) {
                    ticketPrice = 10;
                } else {
                    ticketPrice = 8;
                }
                System.out.println("Ticket price: $"   ticketPrice   "\n");
                System.out.println();

            } else {
                if (rowNumber - 1 <= numberOfRows / 2 - 1) {
                    ticketPrice = 10;
                    System.out.println("Ticket price: $"   ticketPrice   "\n");

                } else {
                    ticketPrice = 8;
                    System.out.println("Ticket price: $"   ticketPrice   "\n");


                }
            }
            break;
        }
    } while(true);
}

I tried creating exceptions to remove the error message , but to no avail , so I switched back to creating a condition which doesn't work for some reason.

CodePudding user response:

else if (rowNumber > numberOfRows - 1 || 
     rowNumber < 0 ||
     seatNumber > numberOfSeatsInEachRow -1 || 
     seatNumber < 0) {
        System.out.println("Wrong input!");

    }

array valid index start from 0 till it's length -1 that why i add -1 also i add the case if user input negative number not sure if you need that also or not

Edit start here :

another issue is the order of the if and if else condition when you use if and if else the first condition return true it's statements will be called

so your first if is

if (cinema[rowNumber - 1][seatNumber].equals("B"))

which will get exception if rowNUmber or seatNumber or both are bigger than the limit or lower than zero

so you need to check the limit before that

you will need to change code a little

public static void pickASeat(String[][] cinema, int numberOfRows, int numberOfSeatsInEachRow, Scanner scanner) {
    System.out.println();
    int rowNumber = 0;
    int seatNumber = 0;
    do {

        System.out.println("Enter a row number:");
        rowNumber = scanner.nextInt();
        System.out.println("Enter a seat number in that row:");
        seatNumber = scanner.nextInt();
        if (rowNumber > numberOfRows - 1 ||
                rowNumber < 0 ||
                seatNumber > numberOfSeatsInEachRow - 1 ||
                seatNumber < 0) {
            System.out.println("Wrong input!");

        } else {
            if (cinema[rowNumber - 1][seatNumber].equals("B")) {
                System.out.println("That ticket has already been purchased!");

            } else {
                System.out.println();
                cinema[rowNumber - 1][seatNumber] = "B";
                int ticketPrice = 0;
                int totalNumberOfSeats = numberOfRows * numberOfSeatsInEachRow;
                int numberOfElementsInArray = 0;
                if (totalNumberOfSeats <= 60) {
                    ticketPrice = 10;
                    System.out.println("Ticket price: $"   ticketPrice);
                } else if (numberOfRows % 2 == 0) {
                    if (rowNumber - 1 <= numberOfRows / 2 - 1) {
                        ticketPrice = 10;
                    } else {
                        ticketPrice = 8;
                    }
                    System.out.println("Ticket price: $"   ticketPrice   "\n");
                    System.out.println();

                } else {
                    if (rowNumber - 1 <= numberOfRows / 2 - 1) {
                        ticketPrice = 10;
                        System.out.println("Ticket price: $"   ticketPrice   "\n");

                    } else {
                        ticketPrice = 8;
                        System.out.println("Ticket price: $"   ticketPrice   "\n");


                    }
                }
                break;
            }
        }
    }
    while (true);
}

and have a nice day :)

  • Related