简体   繁体   中英

How to verify if User Input is equivalent to my element in 2D array

I'm a beginner and I'm trying out error handling in my program. The aim for my program is to 'book' a concert and I created a method to book the seats. However, there are several ways the user can input the wrong number, so I tried to create my own exception and throw it when the user inputs a row or column number that isn't in the seating arrangment.

The problem I'm finding is that I have to use a if statement that checks if the number that the user inputs (which is the index number for the 2d array) is actually in the array elements and what I wrote for this code is not working out.

I want to throw an error that tells the user the inputted an incorrect row and column and then continues on to the rest of the code. This is my code so far..

This is my code for the exception I created.

public class ArrayInputMismatchException extends Exception {
  public ArrayInputMismatchException() {} 

  public ArrayInputMismatchException(String message) {
    super(message); 
  }
  
}

This is my code for my error handling and user input validation. `

    int rowSeat = 0; 
    int colmnSeat = 0; 
    
    try {  
      System.out.println("What is the row number that your seat is in?");  
      rowSeat = userNum.nextInt(); 
      System.out.println("\nWhat is the column number that your seat is in?"); 
      colmnSeat = userNum.nextInt(); 
      arr1[rowSeat - 1][colmnSeat - 1] = 1; //this is to change the element in the array that symbolizes a seat to '1' to show its now booked, before it was 0 to show its available and since they chose this seat, it's now 1... 


      if ((rowSeat || colmnSeat) != arr1[rowSeat][colmnSeat]) { //I put my conditional like this for now, but I'm not sure what else to replace it with to check the user input is equilvalent to the array index
        ArrayInputMismatchException e = new ArrayInputMismatchException("Something went wrong: You cannot enter a number that is not a valid row or column."); 
        throw e;
      } //this is my error message that I want to happen, for example if the seating arrangment in up to a 4x3 and the user inputs row 13, then the error should appear, same with if they enter column 10 then an error message should appear  
      
    } 

    catch (InputMismatchException e) {
      System.out.println("\nAn error occured: You cannot enter any character or symbol other than a valid number for the row and column of the chosen seat for booking. " + e); 
    } //this is an exception that appears when they input a string instead of a number, like 'k' or '?' instead of a valid number present in the rows and columns, it already works so i'm not that worried about it 

`

Thank you in advance to all the help!

TLDR;

Two possible solutions (there can be more).

  1. Manual index check
if (rowSeat < 0 || rowSeat >= arr1.length
    || colmnSeat < 0 || colmnSeat >= arr1[0].length) {
    throw new ArrayInputMismatchException("Something ....");
}
  1. Catching the thrown exception:
...
} 
catch (InputMismatchException e) {
  System.out.println("\nAn error occured: You cannot enter any character or symbol other than a valid number for the row and column of the chosen seat for booking. " + e); 
} //...
catch (ArrayIndexOutOfBoundsException e) {
  System.out.println("your other error message");
  // or....
  throw new ArrayInputMismatchException("your message");
}
...

Further explanation

The if-statement you wrote won't compile. The combination you did there is not possible in java. You could have something like

if (rowSeat != arr1[rowSeat][colmnSeat]
    || colmnSeat != arr1[rowSeat][colmnSeat]) {
  ...
}

but that wouldn't help you. This statement would check if the value in your array is not equal to the users selected row or the column.

Solutions:

You have two options:

  1. you do a manual index check or
  2. you let handle java the index check.

1. The manual index check:

First, you need to check the user input before you assign the value for arr1 . So your if-statement needs to happen right after colmnSeat = userNum.nextInt(); .

You need to do the following:

if (rowSeat < 0 || rowSeat >= arr1.length
    || colmnSeat < 0 || colmnSeat >= arr1[0].length) {
    throw new ArrayInputMismatchException("Something ....");
}

Further explanation

First of all, arrays start at 0. So, given an array of 4, the first element has the index 0 and the last element has the index 3 (not 4.), If you would try to access arr[4]. you would get an error (ArrayIndexOutOfBoundsException) and your program stops.

That's why we need to check for negative input. Secondly we need to check, if the values are greater or equal to the size of the array.

First it checks if the values are negative and then it checks, if the values are out of bound. The 2D-array is actually an array of arrays of this structure:

arr1 = [[row1-col1, row1-col2]]
       [[row2-col1, row2-col2]]
       [[row3-col1, row3-col2]]

wherein row1-col1 is the value of arr1[0,0] . In this structure, the outer array holds the arrays with the values. So when only calling the outer array, you get the array for one row:

arr1[0] == [row1-col1, row1-col2]

That's how these values come about:

arr1.length    == 3
arr1[0].length == 2

2. Letting java handle the check:

Whenever you try to access a value from an array with an index that doesn't exist for the array (either too big or to low), java throws the ArrayIndexOutOfBoundsException . We can take leverage of this fact and catch this error in a second catch block, which can be appended to your code in the following manner:

...
} 
catch (InputMismatchException e) {
  System.out.println("\nAn error occured: You cannot enter any character or symbol other than a valid number for the row and column of the chosen seat for booking. " + e); 
} //...
catch (ArrayIndexOutOfBoundsException e) {
  System.out.println("your other error message");
  // or....
  throw new ArrayInputMismatchException("your message");
}
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM