This question already has an answer here:
这个问题在这里已有答案:
- How to avoid ArrayIndexOutOfBoundsException or IndexOutOfBoundsException? [duplicate] 2 answers
如何避免ArrayIndexOutOfBoundsException或IndexOutOfBoundsException? [重复] 2个答案
I have a few string problems that I need to put together for a complete homework assignment. They all work correctly by themselves, but when I put them together in the main function, the last one that finds the smallest word in a string gives an error. Anyone know why?
我有一些字符串问题,我需要将它们放在一起完成一项完整的家庭作业。它们都可以自己正常工作,但是当我将它们放在main函数中时,找到字符串中最小单词的最后一个会产生错误。谁知道为什么?
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
//Length of Word
String word1 = sc.next();
System.out.println(word1.length());
//Evens in one string odds in the other
String word2 = sc.next();
StringBuilder even = new StringBuilder();
StringBuilder odd = new StringBuilder();
for(int i = 0; i < word2.length(); i++){
if(i % 2 == 0){
even.append(word2.charAt(i));
}
else{
odd.append(word2.charAt(i));
}
}
System.out.println(even + " " + odd);
//Diminishing Suffix
String word3 = sc.next();
for(int j = 0; j < word3.length(); j++){
System.out.print(word3.substring(j, word3.length()) + " ");
}
System.out.printf("\n");
//Letter Replacement
String word4 = sc.next();
String word5 = sc.next();
String word6 = sc.next();
String word7 = word4.replace(word5, word6);
System.out.println(word7);
//How many times x appears in xstring
String word8 = sc.next();
String word9 = sc.next();
int index = word8.indexOf(word9);
int count = 0;
while (index != -1) {
count++;
word8 = word8.substring(index + 1);
index = word8.indexOf(word9);
}
System.out.println(count);
System.out.println();
//Lexicographically smallest word
String Sentence = sc.nextLine();
String[] myWords = Sentence.split(" ");
int shortestLengths, shortestLocation;
shortestLengths=(myWords[1]).length();
shortestLocation=1;
for (int i = 1; i <myWords.length; i++) {
if ((myWords[i]).length() < shortestLengths) {
shortestLengths=(myWords[i]).length();
shortestLocation=i;
}
}
System.out.println(myWords[shortestLocation]);
}
}
Talking about the lexicographically smallest one. Error it gives is:
谈论按字典顺序最小的一个。它给出的错误是:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at StringFun.main(StringFun.java:77)
2 个解决方案
#1
3
This line seems to be throwing the exception:
这条线似乎抛出异常:
shortestLengths=(myWords[1]).length();
So myWords
must not have a second element, meaning the string Sentence
didn't contain a space (" "
) to be split on. Since Sentence
is coming from sc.nextLine()
, the "problem" must be with your input - but the true issue is that you're not handling that case properly.
因此myWords不能有第二个元素,这意味着字符串Sentence不包含要分割的空格(“”)。由于句子来自sc.nextLine(),“问题”必须与您的输入相关 - 但真正的问题是您没有正确处理该案例。
Just glancing at your logic, I don't see why shortestLengths
couldn't start out as Integer.MAX_VALUE
since you're looping over each word and seeing if its length is less than that.
只是看一下你的逻辑,我不明白为什么shortestLengths不能以Integer.MAX_VALUE开头,因为你循环遍历每个单词并查看它的长度是否小于那个。
#2
1
If shortestLengths=(myWords[1]).length();
is line 77, I'd say its because your input doesn't have any spaces in it, and you don't check for this. Also on this line, myWords[1] refers to the second word, since 0 is the first index. Your code here ignores the first word entirely.
如果shortestLengths =(myWords [1])。length();是第77行,我会说因为你的输入中没有任何空格,你不检查这个。同样在这一行,myWords [1]引用第二个单词,因为0是第一个索引。这里的代码完全忽略了第一个单词。
#1
3
This line seems to be throwing the exception:
这条线似乎抛出异常:
shortestLengths=(myWords[1]).length();
So myWords
must not have a second element, meaning the string Sentence
didn't contain a space (" "
) to be split on. Since Sentence
is coming from sc.nextLine()
, the "problem" must be with your input - but the true issue is that you're not handling that case properly.
因此myWords不能有第二个元素,这意味着字符串Sentence不包含要分割的空格(“”)。由于句子来自sc.nextLine(),“问题”必须与您的输入相关 - 但真正的问题是您没有正确处理该案例。
Just glancing at your logic, I don't see why shortestLengths
couldn't start out as Integer.MAX_VALUE
since you're looping over each word and seeing if its length is less than that.
只是看一下你的逻辑,我不明白为什么shortestLengths不能以Integer.MAX_VALUE开头,因为你循环遍历每个单词并查看它的长度是否小于那个。
#2
1
If shortestLengths=(myWords[1]).length();
is line 77, I'd say its because your input doesn't have any spaces in it, and you don't check for this. Also on this line, myWords[1] refers to the second word, since 0 is the first index. Your code here ignores the first word entirely.
如果shortestLengths =(myWords [1])。length();是第77行,我会说因为你的输入中没有任何空格,你不检查这个。同样在这一行,myWords [1]引用第二个单词,因为0是第一个索引。这里的代码完全忽略了第一个单词。