“main”java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:17

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22315206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:53:39  来源:igfitidea点击:

"main" java.lang.StringIndexOutOfBoundsException: String index out of range: 17

javastringswingindexoutofboundsexceptionlang

提问by user3377222

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.

我不断收到此消息,但我无法弄清楚。我是 Java 新手,我正在尝试在由按钮组成的体育场制作 gui 座位。

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!

我很感激任何帮助谢谢!

采纳答案by Dawood ibn Kareem

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.

SEAT_SECTIONS 中的字符串文字之一只有 17 个字符。你需要自己弄清楚是哪一个。最有可能的是,您已经使用制表符而不是空格填充了其中的一些空格。

回答by Scary Wombat

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);
  }

回答by MadProgrammer

Your first problem is at line 6 of SEAT_SECTIONS

你的第一个问题是在第 6 行 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.

哪个太短(27 个字符)而不是所需的 35 个字符。

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

您将在第 12、13 和 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 ROWand COLUMNlengths meet reality and take appropriate action when they don't.

您需要相应地填充这些行。您可能还希望添加一些健全性检查,以确保您的预期ROWCOLUMN长度符合现实,并在不符合时采取适当的措施。

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

更好的解决方案可能是使用二维数组。

回答by ErstwhileIII

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

文件 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

文件位.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

文件 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);
}
}

回答by user3299336

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

没有必要说它会引起混乱