-2

I'm writing a program which is suppose to acting like a ticket finder. It displays a chart of possible seating choices along with their prices and asking if the user would like to select a seat by number or by price. It works like it's suppose to on the seat by number, but when I try to find a seat by price I get an array index out of bounds error. I'm confused because it's suppose to be starting a linear search there at zero. I don't understand why this error is there.

import java.util.Scanner;

public class FindTicket{
  public static void main(String[] args){
    String answer="number";
    Scanner kb=new Scanner(System.in);
    int[][] seats= {
      {10,10,10,10,10,10,10,10,10,10},
      {10,10,10,10,10,10,10,10,10,10},
      {10,10,10,10,10,10,10,10,10,10}, 
      {10,10,20,20,20,20,20,20,10,10}, 
      {10,10,20,20,20,20,20,20,10,10}, 
      {10,10,20,20,20,20,20,20,10,10}, 
      {20,20,30,40,40,40,30,30,20,20}, 
      {20,30,30,40,50,50,40,30,30,20},
      {30,40,50,50,50,50,50,50,40,30}
    };

    printChart(seats);
    do{
      System.out.println("Would you like to choose a seat by number, price, or quit?");
      answer = kb.nextLine();
      if(answer.equals("price")){
        sellSeatbyPrice(seats);}
      if(answer.equals("number")){ 
        sellSeatbyNumber(seats);}
      printChart(seats);
    }while(!answer.equals("quit"));
  }

  public static void printChart(int[][] seats){
    for (int i=0; i<seats.length; i++)
    {
      for(int j=0; j<seats[0].length; j++)
      {
        System.out.printf("%8d", seats[i][j]);
      }
      System.out.println();
    }
  }

  public static int[][] sellSeatbyPrice(int[][] seats){
    Scanner kb=new Scanner(System.in);
    int ticketprice;
    int row = 0, col = 0;
    boolean found = false, seatavaliable=true;
    do{
      System.out.println("What is your prefered ticket price?");
      ticketprice=kb.nextInt();
      while (row<seats.length && !found){
        do{
          if(seats[row][col] == ticketprice){
            found = true;}
          else{
            col++; }  
        }while(col<seats[0].length &&!found);
        if(seats[row][col] == ticketprice){
          found = true;}
        else{
          row++;} 
      }
      if(found){
        seats[row][col] = 0; }
      else {
        System.out.println("Seat not found at specified price.");
        seatavaliable=false;}
    }while(seatavaliable==false);

    return seats;
  }

  public static int[][] sellSeatbyNumber(int[][] seats){
    Scanner kb=new Scanner(System.in);
    int row = 0, col = 0;
    int editedrow, editedcol;
    boolean seatavaliable = true;
    do{
      System.out.println("What is your prefered seat number?  Please enter row then column.");
      row=kb.nextInt();
      col=kb.nextInt();
      editedrow = 9-row;
      editedcol = col - 1;
      if(seats[editedrow][editedcol] > 0){
        seats[editedrow][editedcol] = 0;}
      else{
        System.out.println("Seat is not avaliable.");
        seatavaliable=false;}
    }while(seatavaliable==false);

    return seats;
  }

}
A--C
  • 36,351
  • 10
  • 106
  • 92
SMoore
  • 23
  • 1
  • 6
  • 1
    On what line are you getting your out of bounds error? – Michael Mar 15 '13 at 01:36
  • 4
    Asking people to spot errors in your code is not especially productive. You should use the debugger (or add print statements) to isolate the problem, by tracing the progress of your program, and comparing it to what you expect to happen. As soon as the two diverge, then you've found your problem. (And then if necessary, you should construct a [minimal test-case](http://sscce.org).) – Oliver Charlesworth Mar 15 '13 at 01:37
  • @Michael It says I have an error on 27 and 61. – SMoore Mar 15 '13 at 01:44
  • @Oil Charlesworth For my class we have to use JGrasp and we haven't been taught about the debugger. Is that information online where I can find it? – SMoore Mar 15 '13 at 01:45

2 Answers2

0

It's because of do...while.

When this block of code is done, col will be greater than the length of your array.

Look at the comments:

public static int[][] sellSeatbyPrice(int[][] seats){
    Scanner kb=new Scanner(System.in);
    int ticketprice;
    int row = 0, col = 0;
    boolean found = false, seatavaliable=true;
    do{
      System.out.println("What is your prefered ticket price?");
      ticketprice=kb.nextInt();
      while (row<seats.length && !found){
        do{
          if(seats[row][col] == ticketprice){
            found = true;}
          else{
            col++; }  // this line, in the last iteration, will make col=seats[0].length
        }while(col<seats[0].length &&!found);
        if(seats[row][col] == ticketprice){ //col with value greater than it should.
          found = true;}
        else{
          row++;} 
      }
      if(found){
        seats[row][col] = 0; }
      else {
        System.out.println("Seat not found at specified price.");
        seatavaliable=false;}
    }while(seatavaliable==false);

    return seats;
  }
Jean Waghetti
  • 4,711
  • 1
  • 18
  • 28
  • Instead of using seats.length and seats[0].length I created constants to work instead with the intention that the final iteration would make col – SMoore Mar 15 '13 at 01:57
  • If you update the value of col, then sees it is greater than the maximum value and use again the value of col, it will be out of bounds. No matter if using the array length or a constant. – Jean Waghetti Mar 15 '13 at 02:39
  • I changed the inside do..while to a while and added a line after the first while saying 'col=0;' which seemed to fix the problem. Thank you for your help! – SMoore Mar 15 '13 at 03:04
0

The nextInt() method leaves the \n (end line) symbol and is picked up immediately by nextLine(), skipping over the next input. What you want to do is use nextLine() for everything, and parse it later:

String nextIntString = keyboard.nextLine(); //get the number as a single line
int nextInt = Integer.parseInt(nextIntString); //convert the string to an int

This is by far the easiest way to avoid problems--don't mix your "next" methods. Use only nextLine() and then parse ints or separate words afterwards.


You cannot close another Scanner because it then closes the underlying InputStream, therefore the first Scanner can no longer read from the same InputStream and you get a NoSuchElementException.


Last note: you should always close a Scanner when you are done with it to conserve system resources.

syb0rg
  • 8,057
  • 9
  • 41
  • 81