gpt4 book ai didi

java - StringIndexOutOfBoundsException 的问题

转载 作者:行者123 更新时间:2023-12-02 05:20:41 26 4
gpt4 key购买 nike

我必须创建一个数字内存游戏,但如果用户输入错误的数字,我会收到 StringIndexOutOfBoundsException。我看了很多例子,但仍然不知道如何解决它。这发生在 actionPerformed 方法中,使用这行代码 newNumber = answer.substring(counterOne, counterTwo); 任何帮助将不胜感激。

import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class MemoryGame extends Applet implements ActionListener, Runnable {

private Label prompt;
private Button button;
private TextField input, numDisplay;
private int arrayCounter = 3, backgroundColor =0;
private int[] myArray = new int[arrayCounter];
private int[] compare = new int[arrayCounter];
private Thread delayThread;
private boolean running = true, isCompare;


public void init(){
prompt = new Label("Enter the numbers!");
input = new TextField(20);
numDisplay = new TextField(20);
numDisplay.setEditable(false);
button = new Button("Enter");

add(prompt);
add(input);
add(numDisplay);
add(button);
button.addActionListener(this);

populateArray();
delayThread = new Thread(this);
delayThread.start();


}
public void paint(Graphics g){

if(backgroundColor == 1){
setBackground (Color.green);
}
else if(backgroundColor == 2){
setBackground (Color.red);
}
}


public void populateArray() {
GenerateNumbers a = new GenerateNumbers();
myArray = a.returnArray();


int count = 1;
for(int i = 0; i < myArray.length;i++){
System.out.println("index: " + count + " : " + myArray[i]);
count++;
numDisplay.setText(" ");
} //for testing purpose
}

@Override
public void actionPerformed(ActionEvent e)throws StringIndexOutOfBoundsException {
String answer ="", newNumber="";
String[] stringArray = new String[arrayCounter];
int counterOne = 0, counterTwo = 2;
try{
answer = input.getText();
for(int i = 0; i < arrayCounter;i++){
newNumber = answer.substring(counterOne, counterTwo);
stringArray[i] = newNumber;
counterOne += 3;
counterTwo += 3;
}
}catch(NumberFormatException ex){
ex.printStackTrace();
}

for(int i = 0; i < arrayCounter;i++){
compare[i] = Integer.parseInt(stringArray[i]);
}

for(int i = 0; i < arrayCounter;i++){
if(myArray[i] != compare[i]){
backgroundColor = 2;
input.setText("Wrong");
repaint();
}
else{
backgroundColor =1;
input.setText("RIGHT!");//testing
repaint();
}

}

}

public void destroy(){
running = false;
}

@Override
public void run() {
while(running){
String numberString = "" ,stringTwo = " ";
for(int i = 0; i < arrayCounter;i++){
numberString += myArray[i] + stringTwo;
numDisplay.setText(numberString);
}
try{
delayThread.sleep(3500);
}catch(InterruptedException e){
e.printStackTrace();
Thread.currentThread().interrupt();
}
numDisplay.setText(" ");
destroy();
}
}

}

最佳答案

在调用 answer.substring(counterOne, counterTwo) 之前,您必须确保0 <= counterOne < answer.length()counterTwo <= answer.length() .

substring会抛出IndexOutOfBoundsException如果the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex .

编辑:

让我们尝试解决您的问题。以下循环的逻辑有缺陷:

        answer = input.getText();
for(int i = 0; i < arrayCounter;i++){
newNumber = answer.substring(counterOne, counterTwo);
stringArray[i] = newNumber;
counterOne += 3;
counterTwo += 3;
}

如果我理解你想要做什么,你期望输入字符串(存储为 answer )包含数字,你创建这些数字的数组,并将其与正确数字的数组进行比较。

但是,只有当用户的答案以“11x22x33...”之类的内容开头时,上述循环才有效,因为您希望提取 arrayCounter (=3) 个来自该输入字符串的每个 2 个字符的子字符串,并且您将从字符串的位置 (0,1) (3,4) (6,7) 中提取它们。这意味着如果字符串少于 8 个字符,您将得到 StringIndexOutOfBoundsException .

您不应假设输入字符串中数字的长度或数字的数量。更好、更简单的解决方案是:

stringArray = answer.split(" ");

这会在空格处分割输入数组。请注意,您将获得的数组的长度不一定与正确答案的长度匹配,因此您必须比较长度。

关于java - StringIndexOutOfBoundsException 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26547308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com