Java 爪哇的刽子手将数组索引抛出边界
我的程序在这段代码中抛出StringIndexOutOfBoundsException:Java 爪哇的刽子手将数组索引抛出边界,java,string,for-loop,while-loop,indexoutofboundsexception,Java,String,For Loop,While Loop,Indexoutofboundsexception,我的程序在这段代码中抛出StringIndexOutOfBoundsException:temp1=temp.replace('-',temp.charAt(p))我正在尝试获取同一个字母的索引(在比较输入的字母和单词之后),并删除“-”,以表明用户猜对了。****我已经尝试了几个小时,但没有成功。我认为问题在于我的循环。谢谢你的回答:)如果我违反了什么,请原谅我 三, 位于java.lang.String.charAt(String.java:658) 在Hangman.main(Hangma
temp1=temp.replace('-',temp.charAt(p))代码>我正在尝试获取同一个字母的索引(在比较输入的字母和单词之后),并删除“-”,以表明用户猜对了。****我已经尝试了几个小时,但没有成功。我认为问题在于我的循环。谢谢你的回答:)如果我违反了什么,请原谅我
三,
位于java.lang.String.charAt(String.java:658)
在Hangman.main(Hangman.java:34)
Java结果:1
import java.util.Scanner;
公共级刽子手{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串word=“Sample”;
字符串temp=null;
字符串temp1=null;
字符串字母=null;
int n;
int m=0;
int p=0;
对于(n=0;n
单词.indexOf(letter);如果后者以字符串形式存在,则返回字母索引,否则返回-1。这就是为什么会出现异常。执行此操作时:
temp = word.replaceAll(word, "-");
……您设置的代码> TEMP只是“--”,而不是(例如)“-----”要知道为什么,考虑<代码> Word < /C> >“hello”;然后这行看起来像:
temp = "hello".replaceAll("hello", "-");
因此,稍后您将假设temp
与word
一样长,因为您在word
中找到索引,并尝试在temp
中访问该字符。但是temp
只有一个字符长,因此出现异常
p = word.indexOf(letter);
temp1 = temp.replace('-', temp.charAt(p));
你应该检查replaceAll()方法的文档,因为你用错了
replaceAll(字符串正则表达式、字符串替换)
将此字符串中与给定正则表达式匹配的每个子字符串替换为给定替换
将整个字符串放入regex参数中
如果执行myString.replaceAll(“\\”,“-”);
(使用双反斜杠指定正则表达式)将换行符旁边的任何字符替换为“-”签入正则表达式。试试这个。。。。。
这将解决您的问题
包装豆;
导入java.util.Scanner
公共级刽子手{
public static String replace(String str, int index, char replace){
if(str==null){
return str;
}else if(index<0 || index>=str.length()){
return str;
}
char[] chars = str.toCharArray();
chars[index] = replace;
return String.valueOf(chars);
}
public static void main (String [] args){
Scanner sc = new Scanner(System.in);
String word = "Sample";
String temp = "";
String letter = null;
int n;
int m=0;
int p = 0;
for (n = 0; n<word.length(); n++){
temp = temp + word.replaceAll(word, "-"); //replaces the String word with "-" and prints
}
System.out.print(temp);
while (m <= 5){ //the player can only guess incorrectly 5 times
System.out.println("\nEnter a letter:");
letter = sc.nextLine();
letter.toLowerCase();
if (word.contains(letter) == true){
p = word.indexOf(letter);
temp = replace(temp, p , word.charAt(p)); //if the word contains the letter, "-" is replaced by the letter.
System.out.println(temp);
}
else {
System.out.print("\nMissed: "+letter); //if not, Missed: +the given letter
m++; //to count for incorrect guesses
}
}
System.out.println("Game Over.");
}
publicstaticstringreplace(stringstr,int-index,charreplace){
如果(str==null){
返回str;
}else if(index=str.length()){
返回str;
}
char[]chars=str.toCharArray();
字符[索引]=替换;
返回字符串.valueOf(字符);
}
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串word=“Sample”;
字符串temp=“”;
字符串字母=null;
int n;
int m=0;
int p=0;
对于(n=0;这是错误的,它排除了单词中的最后一个索引。数组的边界是0是的,这并不能解决问题。它删除字符串的最后一个索引。temp1=temp.replace('-',temp.charAt(p));在这一行中,使用word.charAt(p)代替temp.charAt(p)因为当你写temp=word.replaceAll(word,“-”)时,temp是------而不是sample;这将帮助你……啊,是的,我现在可以看到了。所以我尝试将temp1=temp.replace('-',temp.charAt(p));替换为temp1=temp.replace('-',word charAt(p));。但实际情况是这样的:-----输入字母:aa输入字母:它只打印到索引。我似乎无法正确实现“逐字符打印”,我认为这可能会解决这个问题。它打印两次,因为您正在编写System.out.print(temp1);如果(m我试着按照你的建议做了,但我仍然坚持打印直到返回索引。是的,这解决了问题!非常感谢!)
temp = "hello".replaceAll("hello", "-");
p = word.indexOf(letter);
temp1 = temp.replace('-', temp.charAt(p));
public static String replace(String str, int index, char replace){
if(str==null){
return str;
}else if(index<0 || index>=str.length()){
return str;
}
char[] chars = str.toCharArray();
chars[index] = replace;
return String.valueOf(chars);
}
public static void main (String [] args){
Scanner sc = new Scanner(System.in);
String word = "Sample";
String temp = "";
String letter = null;
int n;
int m=0;
int p = 0;
for (n = 0; n<word.length(); n++){
temp = temp + word.replaceAll(word, "-"); //replaces the String word with "-" and prints
}
System.out.print(temp);
while (m <= 5){ //the player can only guess incorrectly 5 times
System.out.println("\nEnter a letter:");
letter = sc.nextLine();
letter.toLowerCase();
if (word.contains(letter) == true){
p = word.indexOf(letter);
temp = replace(temp, p , word.charAt(p)); //if the word contains the letter, "-" is replaced by the letter.
System.out.println(temp);
}
else {
System.out.print("\nMissed: "+letter); //if not, Missed: +the given letter
m++; //to count for incorrect guesses
}
}
System.out.println("Game Over.");
}