title: Seat Reservation System two-dimensional array - Java
body: <p>I am tasked to create a seat reservation system using two-dimensional arrays. The program specifications are the ff:</p>
  <ol>
  <li>Use two-dimensional array with size 7 columns and 5 rows.</li>
  <li>Seat numbers are populated during run-time and not hard-coded.</li>
  <li>User is asked to input a seat number.</li>
  <li>The chosen seat number is replaced by the letter X.</li>
  <li>Program displays a remark “Seat successfully reserved” when reservation is
  done.</li>
  <li>The user is not allowed to reserve a previously reserved seat. Display “Seat is
  taken” remarks.</li>
  <li>The user is not allowed to enter an invalid seat number. Display an error
  message.</li>
  <li>The program continuously loops.</li>
  </ol>
  <p>The program works at the start but when it loops again the next seat number doesn't get reserved and just displays the seat is taken.</p>
  <p>The code:</p>
  <pre><code>
  public class SeatReservation_Andaya {
  
      // create 2d array w/ 7 columns and 5 rows
      static int[][] seatArray = new int[5][7];
  
      // create another array 
      static String[][] stringArray = new String[5][7];
  
      // declare variables 
      static int seatNum = 1;
      static int i, j;
  
      static boolean chkData() {  // Method that will check for reservation availability
          for (i = 0; i &lt; 5; ++i) {
              for (j = 0; j &lt; 7; ++j) {
                  if (stringArray[i][j].equals(&quot;X&quot;)) {
                      return true;
                  }
              }
          }
          return false;
      }
  
      static void replaceWithX(int numRes) {
          // replaces the seat chosen with &quot;X&quot;
          switch (numRes) {
              case 1:
                  stringArray[0][0] = &quot;X&quot;;
                  break;
  
              case 2:
                  stringArray[0][1] = &quot;X&quot;;
                  break;
  
              case 3:
                  stringArray[0][2] = &quot;X&quot;;
                  break;
  
              case 4:
                  stringArray[0][3] = &quot;X&quot;;
                  break;
  
              case 5:
                  stringArray[0][4] = &quot;X&quot;;
                  break;
  
              case 6:
                  stringArray[0][5] = &quot;X&quot;;
                  break;
  
              case 7:
                  stringArray[0][6] = &quot;X&quot;;
                  break;
  
              case 8:
                  stringArray[1][0] = &quot;X&quot;;
                  break;
  
              case 9:
                  stringArray[1][1] = &quot;X&quot;;
                  break;
  
              case 10:
                  stringArray[1][2] = &quot;X&quot;;
                  break;
  
              case 11:
                  stringArray[1][3] = &quot;X&quot;;
                  break;
  
              case 12:
                  stringArray[1][4] = &quot;X&quot;;
                  break;
  
              case 13:
                  stringArray[1][5] = &quot;X&quot;;
                  break;
  
              case 14:
                  stringArray[1][6] = &quot;X&quot;;
                  break;
  
              case 15:
                  stringArray[2][0] = &quot;X&quot;;
                  break;
  
              case 16:
                  stringArray[2][1] = &quot;X&quot;;
                  break;
  
              case 17:
                  stringArray[2][2] = &quot;X&quot;;
                  break;
  
              case 18:
                  stringArray[2][3] = &quot;X&quot;;
                  break;
  
              case 19:
                  stringArray[2][4] = &quot;X&quot;;
                  break;
  
              case 20:
                  stringArray[2][5] = &quot;X&quot;;
                  break;
  
              case 21:
                  stringArray[2][6] = &quot;X&quot;;
                  break;
  
              case 22:
                  stringArray[3][0] = &quot;X&quot;;
                  break;
  
              case 23:
                  stringArray[3][1] = &quot;X&quot;;
                  break;
  
              case 24:
                  stringArray[3][2] = &quot;X&quot;;
                  break;
  
              case 25:
                  stringArray[3][3] = &quot;X&quot;;
                  break;
  
              case 26:
                  stringArray[3][4] = &quot;X&quot;;
                  break;
  
              case 27:
                  stringArray[3][5] = &quot;X&quot;;
                  break;
  
              case 28:
                  stringArray[3][6] = &quot;X&quot;;
                  break;
  
              case 29:
                  stringArray[4][0] = &quot;X&quot;;
                  break;
  
              case 30:
                  stringArray[4][1] = &quot;X&quot;;
                  break;
  
              case 31:
                  stringArray[4][2] = &quot;X&quot;;
                  break;
  
              case 32:
                  stringArray[4][3] = &quot;X&quot;;
                  break;
  
              case 33:
                  stringArray[4][4] = &quot;X&quot;;
                  break;
  
              case 34:
                  stringArray[4][5] = &quot;X&quot;;
                  break;
  
              case 35:
                  stringArray[4][6] = &quot;X&quot;;
                  break;
          }
      }
  
      public static void main(String[] args) {
          // create scanner tool
          Scanner input = new Scanner(System.in);
  
          // declare values of int array 
          for (int i = 0; i &lt; 5; i++) { // loop through all rows
              for (int j = 0; j &lt; 7; j++) { // loop through all elements of current row
                  seatArray[i][j] = seatNum;
                  seatNum++;
              }
          }
  
          // puts int array values into string
          for (int i = 0; i &lt; 5; i++) { // loop through all rows
              for (int j = 0; j &lt; 7; j++) { // loop through all elements of current row
                  stringArray[i][j] = Integer.toString(seatArray[i][j]);
              }
          }
  
          // prints the array
          for (int i = 0; i &lt; 5; i++) { // loop through all rows
              for (int j = 0; j &lt; 7; j++) { // loop through all elements of current row
                  System.out.print(stringArray[i][j] + &quot;\t&quot;);
              }
              System.out.println();
          }
  
          // loops reservation system 
          while (true) {
              // asks for seat number
              System.out.print(&quot;Enter seat number to reserve: &quot;);
  
              // reads input
              int numRes = Integer.valueOf(input.nextLine());
  
              // check if input is invalid
              if (numRes &gt; 35 || numRes &lt;1) {
                  System.out.println(&quot;ERROR: INVALID SEAT NUMBER&quot;);
              } else {
                  // checks if seat is reserved
                  if (chkData()) {
                      System.out.println(&quot;Seat is taken&quot;);
                  } 
                  // reserves seat if not reserved
                  else {
                      replaceWithX(numRes);
                      System.out.println(&quot;Seat successfully reserved&quot;);
                  }
              }
              // prints new array
              for (int i = 0; i &lt; 5; i++) { // loop through all rows
                  for (int j = 0; j &lt; 7; j++) { // loop through all elements of current row
                      System.out.print(stringArray[i][j] + &quot;\t&quot;);
                  }
                  System.out.println();
              }
          }
      }
  }
  </code></pre>
  <p>The output is like this looks like this:</p>
  <p>1   2   3   4   5   6   7<br />
  8   9   10  11  12  13  14<br />
  15  16  17  18  19  20  21<br />
  22  23  24  25  26  27  28<br />
  29  30  31  32  33  34  35<br />
  Enter seat number to reserve: 1
  Seat successfully reserved</p>
  <p>X   2   3   4   5   6   7<br />
  8   9   10  11  12  13  14<br />
  15  16  17  18  19  20  21<br />
  22  23  24  25  26  27  28<br />
  29  30  31  32  33  34  35<br />
  Enter seat number to reserve: 2
  Seat is taken</p>
  <p>X   2   3   4   5   6   7<br />
  8   9   10  11  12  13  14<br />
  15  16  17  18  19  20  21<br />
  22  23  24  25  26  27  28<br />
  29  30  31  32  33  34  35<br />
  Enter seat number to reserve:</p>
  <p>Can you guys give me tips on where the problem in the code is? Thank you!</p>