0
Exception in thread "main" `java.lang.StringIndexOutOfBoundsException: String index out of range: 17`        at java.lang.String.charAt(String.java:658)
    at Stadium.<init>(Stadium.java:104)
    at StadiumPanel.main(StadiumPanel.java:53)

I keep getting this message and I can't figure it out. I'm new to java and I'm trying to make gui of seats at a stadium made of buttons.

This is my model code:

public class Stadium { 
 public static int ROWS = 27; 
 public static int COLUMNS = 35; 

 private static String[] SEAT_NUMBERS = { 
 "9 1234567890 123456789 1234567890 1", 
 "8 1234567890 123456789 1234567890 2", 
 "7 1234567890 123456789 1234567890 3", 
 "6                                 4", 
 "55   12345678 1234567 12345678   15", 
 "44 8  1234567 1234567 1234567  1 26", 
 "33 77                         12 37", 
 "22 66   1234561234567123456   23 48", 
 "11 55 0  12345123456712345  1 34 59", 
 "99 44 98  123412345671234  12 45 11", 
 "88 33 876 --------------- 123 56 22", 
 "77 22 765|               |234 67 33", 
 "66 11 654|               |345 78 44", 
 "55       |               |       55", 
 "44 87 543|               |456 11 66", 
 "33 76 432|               |567 22 77", 
 "22 65 321 --------------- 678 33 88", 
 "11 54 21  432176543214321  89 44 99", 
 "95 43 1  54321765432154321  0 55 11", 
 "84 32   6543217654321654321   66 22", 
 "73 21                         77 33", 
 "62 1  7654321 7654321 7654321  8 44", 
 "51   87654321 7654321 87654321   55", 
 "4                                 6", 
 "3 0987654321 987654321 0987654321 7", 
 "2 0987654321 987654321 0987654321 8", 
 "1 0987654321 987654321 0987654321 9" 
 }; 

 private static String[] SEAT_ROWS = { 
 "Z CCCCCCCCCC FFFFFFFFF IIIIIIIIII K", 
 "Z BBBBBBBBBB EEEEEEEEE HHHHHHHHHH K", 
 "Z AAAAAAAAAA DDDDDDDDD GGGGGGGGGG K", 
 "Z                                 K", 
 "ZY   BBBBBBBB DDDDDDD FFFFFFFF   JK", 
 "ZY T  AAAAAAA CCCCCCC EEEEEEE  H JK", 
 "ZY TS                         GH JK", 
 "ZY TS   CCCCCCFFFFFFFIIIIII   GH JK", 
 "ZY TS X  BBBBBEEEEEEEHHHHH  L GH JK", 
 "DC TS XW  AAAADDDDDDDGGGG  KL GH AB", 
 "DC TS XWV --------------- JKL GH AB", 
 "DC TS XWV|               |JKL GH AB", 
 "DC TS XWV|               |JKL GH AB", 
 "DC       |               |       AB", 
 "DC RQ XWV|               |JKL IJ AB", 
 "DC RQ XWV|               |JKL IJ AB", 
 "DC RQ XWV --------------- JKL IJ AB", 
 "DC RQ XW  SSSSPPPPPPPMMMM  KL IJ AB", 
 "XW RQ X  TTTTTQQQQQQQNNNNN  L IJ LM", 
 "XW RQ   UUUUUURRRRRRROOOOOO   IJ LM", 
 "XW RQ                         IJ LM", 
 "XW R  OOOOOOO MMMMMMM KKKKKKK  J LM", 
 "XW   PPPPPPPP NNNNNNN LLLLLLLL   LM", 
 "X                                 M", 
 "X TTTTTTTTTT QQQQQQQQQ NNNNNNNNNN M", 
 "X UUUUUUUUUU RRRRRRRRR OOOOOOOOOO M", 
 "X VVVVVVVVVV SSSSSSSSS PPPPPPPPPP M" 
 }; 

 private static String[] SEAT_SECTIONS = { 
 "3 3333333333 333333333 3333333333 3", 
 "3 3333333333 333333333 3333333333 3", 
 "3 3333333333 333333333 3333333333 3", 
 "3                                 3", 
 "33   22222222 2222222 22222222   33", 
 "33 2  2222222 2222222 2222222  2 33", 
 "33 22                 22 33", 
 "33 22   1111111111111111111   22 33", 
 "33 22 1  11111111111111111  1 22 33", 
 "44 22 11  111111111111111  11 22 44", 
 "44 22 111 --------------- 111 22 44", 
 "44 22 111|               |111 22 44", 
 "44 22 111|           |111 22 44", 
 "44       |           |       44", 
 "44 22 111|               |111 22 44", 
 "44 22 111|           |111 22 44", 
 "44 22 111 --------------- 111 22 44", 
 "44 22 11  111111111111111  11 22 44", 
 "33 22 1  11111111111111111  1 22 33", 
 "33 22   1111111111111111111   22 33", 
 "33 22                         22 33", 
 "33 2  2222222 2222222 2222222  2 33", 
 "33   22222222 2222222 22222222   33", 
 "3                                 3", 
 "3 3333333333 333333333 3333333333 3", 
 "3 3333333333 333333333 3333333333 3", 
 "3 3333333333 333333333 3333333333 3" 
 }; 

 private Seat[][] seats; 

 public Stadium() { 
     seats = new Seat[ROWS][COLUMNS]; 

     for (int r=0; r< ROWS; r++) {  String secString = SEAT_SECTIONS[r]; 
         String rowString = SEAT_ROWS[r]; 
         String numString = SEAT_NUMBERS[r]; 
         for (int c=0; c< COLUMNS; c++) { 
              -->byte section =        (byte)Character.digit(secString.charAt(c),10); 
             char row = (char)rowString.charAt(c); 
             byte number = (byte)Character.digit(numString.charAt(c),10); 
             if (!Character.isLetter(row)) 
                seats[r][c] = null; 
             else 
                seats[r][c] = new Seat(section, row, number); 
         } 
     } 
 } 

 public Seat[][] getSeats() { return seats; } 
 public Seat getSeat(int row, int col) { return seats[row][col]; } 

}

This is my panel code:

import java.awt.*; 
import javax.swing.*; 

public class StadiumPanel extends JPanel {
private Stadium     model;

public Seat[][] seatButtons = new Seat[27][35];

public StadiumPanel(Stadium m){
    model = m;

    Seat[][] seatButtons = m.getSeats();

    setLayout(new GridLayout(27,35));
    setBackground(Color.WHITE);

    for (int row=0; row<27; row++){
        for(int col=0; col<35; col++){
            if (seatButtons[row][col] != null){
                if (seatButtons[row][col].getSection() == 1){
                    JButton b = new JButton();
                    b.setBackground(Color.RED);
                    add(b);
                }
                else if (seatButtons[row][col].getSection() == 2){
                    JButton b = new JButton();
                    b.setBackground(Color.GREEN);
                    add(b);
                }
                else if (seatButtons[row][col].getSection() == 3){
                    JButton b = new JButton();
                    b.setBackground(Color.BLUE);
                    add(b);
                }
                else if (seatButtons[row][col].getSection() == 4){
                    JButton b = new JButton();
                    b.setBackground(Color.YELLOW);
                    add(b);
                }

            }
            else if (seatButtons[row][col] == null){
                JLabel b = new JLabel();
                b.setBackground(Color.WHITE);
                add(b);
            }
        }
    }   
}

public static void main(String args[]) { 
     JFrame f = new JFrame("Stadium Panel Test"); 
      -->f.getContentPane().add(new StadiumPanel(new Stadium())); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setSize(649, 500); 
     f.setVisible(true); 
} 

}

I'd appreciate any help thanks!

2
  • I just added arrows to the lines that give me errors Commented Mar 11, 2014 at 2:26
  • Can you post your Seat class? Commented Mar 11, 2014 at 2:31

5 Answers 5

2

Your first problem is at line 6 of SEAT_SECTIONS

    "33 2  2222222 2222222 2222222  2 33",
    "33 22                 22 33", // Line 6
    "33 22   1111111111111111111   22 33",

Which is too short (27 characters) instead of the required 35.

You'll have the same problem on lines 12, 13 and 15.

    "44 22 111|           |111 22 44", // Line 12
    "44       |           |       44", // Line 13
    "44 22 111|               |111 22 44",
    "44 22 111|           |111 22 44", // Line 15

You'll need to pad these lines out accordingly. You may also wish to add some sanity checking in to ensure that your expected ROW and COLUMN lengths meet reality and take appropriate action when they don't.

A better solution might be to use a two dimensional array.

Sign up to request clarification or add additional context in comments.

4 Comments

why would the error say String index out of range: 17 then?
@user2310289 I can only assume that there is a difference between what you posted and what is running, because I always get Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 27 - It could be the conversion between what you have in your editor, how it's saved to SO and how it copies to mine...
Sorry for the confusion but I am not the OP, just a curious but anonymous type of user.
@user2310289 Sorry for jumping to conclusions ;)
1

One of your String literals in SEAT_SECTIONS has only 17 characters. You'll need to work out for yourself which one. Most likely, you've filled in some of those blank spaces using tab characters instead of spaces.

Comments

1

I tried to recreate your problem .. but things seem to be working. The files I used are below -- they display the seating chart with different colored buttons. I made only minor changes (using "model" in Stadium panel; formatting.

File Stadium.java

package com.example.seats;
public class Stadium { 
 public static int ROWS = 27; 
 public static int COLUMNS = 35; 

 private static String[] SEAT_NUMBERS = { 
 "9 1234567890 123456789 1234567890 1", 
 "8 1234567890 123456789 1234567890 2", 
 "7 1234567890 123456789 1234567890 3", 
 "6                                 4", 
 "55   12345678 1234567 12345678   15", 
 "44 8  1234567 1234567 1234567  1 26", 
 "33 77                         12 37", 
 "22 66   1234561234567123456   23 48", 
 "11 55 0  12345123456712345  1 34 59", 
 "99 44 98  123412345671234  12 45 11", 
 "88 33 876 --------------- 123 56 22", 
 "77 22 765|               |234 67 33", 
 "66 11 654|               |345 78 44", 
 "55       |               |       55", 
 "44 87 543|               |456 11 66", 
 "33 76 432|               |567 22 77", 
 "22 65 321 --------------- 678 33 88", 
 "11 54 21  432176543214321  89 44 99", 
 "95 43 1  54321765432154321  0 55 11", 
 "84 32   6543217654321654321   66 22", 
 "73 21                         77 33", 
 "62 1  7654321 7654321 7654321  8 44", 
 "51   87654321 7654321 87654321   55", 
 "4                                 6", 
 "3 0987654321 987654321 0987654321 7", 
 "2 0987654321 987654321 0987654321 8", 
 "1 0987654321 987654321 0987654321 9" 
 }; 

 private static String[] SEAT_ROWS = { 
 "Z CCCCCCCCCC FFFFFFFFF IIIIIIIIII K", 
 "Z BBBBBBBBBB EEEEEEEEE HHHHHHHHHH K", 
 "Z AAAAAAAAAA DDDDDDDDD GGGGGGGGGG K", 
 "Z                                 K", 
 "ZY   BBBBBBBB DDDDDDD FFFFFFFF   JK", 
 "ZY T  AAAAAAA CCCCCCC EEEEEEE  H JK", 
 "ZY TS                         GH JK", 
 "ZY TS   CCCCCCFFFFFFFIIIIII   GH JK", 
 "ZY TS X  BBBBBEEEEEEEHHHHH  L GH JK", 
 "DC TS XW  AAAADDDDDDDGGGG  KL GH AB", 
 "DC TS XWV --------------- JKL GH AB", 
 "DC TS XWV|               |JKL GH AB", 
 "DC TS XWV|               |JKL GH AB", 
 "DC       |               |       AB", 
 "DC RQ XWV|               |JKL IJ AB", 
 "DC RQ XWV|               |JKL IJ AB", 
 "DC RQ XWV --------------- JKL IJ AB", 
 "DC RQ XW  SSSSPPPPPPPMMMM  KL IJ AB", 
 "XW RQ X  TTTTTQQQQQQQNNNNN  L IJ LM", 
 "XW RQ   UUUUUURRRRRRROOOOOO   IJ LM", 
 "XW RQ                         IJ LM", 
 "XW R  OOOOOOO MMMMMMM KKKKKKK  J LM", 
 "XW   PPPPPPPP NNNNNNN LLLLLLLL   LM", 
 "X                                 M", 
 "X TTTTTTTTTT QQQQQQQQQ NNNNNNNNNN M", 
 "X UUUUUUUUUU RRRRRRRRR OOOOOOOOOO M", 
 "X VVVVVVVVVV SSSSSSSSS PPPPPPPPPP M" 
 }; 

 private static String[] SEAT_SECTIONS = { 
 "3 3333333333 333333333 3333333333 3", 
 "3 3333333333 333333333 3333333333 3", 
 "3 3333333333 333333333 3333333333 3", 
 "3                                 3", 
 "33   22222222 2222222 22222222   33", 
 "33 2  2222222 2222222 2222222  2 33", 
 "33 22                         22 33", 
 "33 22   1111111111111111111   22 33", 
 "33 22 1  11111111111111111  1 22 33", 
 "44 22 11  111111111111111  11 22 44", 
 "44 22 111 --------------- 111 22 44", 
 "44 22 111|               |111 22 44", 
 "44 22 111|               |111 22 44", 
 "44       |               |       44", 
 "44 22 111|               |111 22 44", 
 "44 22 111|               |111 22 44", 
 "44 22 111 --------------- 111 22 44", 
 "44 22 11  111111111111111  11 22 44", 
 "33 22 1  11111111111111111  1 22 33", 
 "33 22   1111111111111111111   22 33", 
 "33 22                         22 33", 
 "33 2  2222222 2222222 2222222  2 33", 
 "33   22222222 2222222 22222222   33", 
 "3                                 3", 
 "3 3333333333 333333333 3333333333 3", 
 "3 3333333333 333333333 3333333333 3", 
 "3 3333333333 333333333 3333333333 3" 
 }; 

 private Seat[][] seats; 

 public Stadium() { 
     seats = new Seat[ROWS][COLUMNS]; 

     for (int r=0; r< ROWS; r++) {  String secString = SEAT_SECTIONS[r]; 
         String rowString = SEAT_ROWS[r]; 
         String numString = SEAT_NUMBERS[r]; 
         for (int c=0; c< COLUMNS; c++) { 
             byte section = (byte)Character.digit(secString.charAt(c),10); 
             char row = (char)rowString.charAt(c); 
             byte number = (byte)Character.digit(numString.charAt(c),10); 
             if (!Character.isLetter(row)) 
                seats[r][c] = null; 
             else 
                seats[r][c] = new Seat(section, row, number); 
         } 
     } 
 } 

 public Seat[][] getSeats() { return seats; } 
 public Seat getSeat(int row, int col) { return seats[row][col]; }


}

File Seat.java

package com.example.seats;

public class Seat {
byte section;
char row;
byte number;

public Seat(byte section, char row, byte number) {
    this.section = section;
    this.row = row;
    this.number = number;
}

public int getSection() {
    return section;
}
}

File StadiumPanel.java

package com.example.seats;

import java.awt.*;
import javax.swing.*;

public class StadiumPanel extends JPanel {
private Stadium model;

public Seat[][] seatButtons = new Seat[27][35];

public StadiumPanel(Stadium m) {
    model = m;

    Seat[][] seatButtons = model.getSeats();

    setLayout(new GridLayout(27, 35));
    setBackground(Color.WHITE);

    for (int row = 0; row < 27; row++) {
        for (int col = 0; col < 35; col++) {
            if (seatButtons[row][col] != null) {
                if (seatButtons[row][col].getSection() == 1) {
                    JButton b = new JButton();
                    b.setBackground(Color.RED);
                    add(b);
                } else if (seatButtons[row][col].getSection() == 2) {
                    JButton b = new JButton();
                    b.setBackground(Color.GREEN);
                    add(b);
                } else if (seatButtons[row][col].getSection() == 3) {
                    JButton b = new JButton();
                    b.setBackground(Color.BLUE);
                    add(b);
                } else if (seatButtons[row][col].getSection() == 4) {
                    JButton b = new JButton();
                    b.setBackground(Color.YELLOW);
                    add(b);
                }

            } else if (seatButtons[row][col] == null) {
                JLabel b = new JLabel();
                b.setBackground(Color.WHITE);
                add(b);
            }
        }
    }
}

public static void main(String args[]) {
    JFrame f = new JFrame("Stadium Panel Test");
    f.getContentPane().add(new StadiumPanel(new Stadium()));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(649, 500);
    f.setVisible(true);
}
}

Comments

0

To quickly find out where in your data you have a problem

use

  for (int c=0; c< secString .length(); c++) { 

or test the length of this string first.

  if (secString.length() != COLUMNS) {
     System.out.println ("error with row " + r);
  }

Comments

0

i had a similar problem just make sure your stadium class is alligned well. matching exactly how it looks like on the assignment. oh and take out this

 public Seat[][] seatButtons = new Seat[27][35]

and change to

  public Seat[][] seatButtons = new Seat[][]

it wasn't necessory to put that it causes confussion

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.