[英]String index out of bounds? (Java, substring loop)
This program I'm making for a COSC course isn't compiling right, I keep getting the error: 我正在为COSC课程制作的该程序编译不正确,但我不断收到错误消息:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2 线程“主”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:2
at java.lang.String.substring(String.java:1765) at VowelCount.main(VowelCount.java:13) 在VowelCount.main(VowelCount.java:13)在java.lang.String.substring(String.java:1765)
Here's my code: 这是我的代码:
import java.util.Scanner;
public class VowelCount {
public static void main(String[] args) {
int a = 0, e = 0, i = 0, o = 0, u = 0, count = 0;
String input, letter;
Scanner scan = new Scanner (System.in);
System.out.println ("Please enter a string: ");
input = scan.nextLine();
while (count <= input.length() ) {
letter = input.substring(count, (count + 1));
if (letter == "a") {
a++; }
if (letter == "e") {
e++; }
if (letter == "i") {
i++; }
if (letter == "o") {
o++; }
if (letter == "u") {
u++; }
count++;
}
System.out.println ("There are " + a + " a's.");
System.out.println ("There are " + e + " e's.");
System.out.println ("There are " + i + " i's.");
System.out.println ("There are " + o + " o's.");
System.out.println ("There are " + u + " u's.");
}
}
To my knowledge this should work, but why doesn't it? 据我所知,这应该起作用,但是为什么不起作用呢? Any help would be great.
任何帮助都会很棒。 Thank you!
谢谢!
You may need to take out the = in the line 您可能需要删除行中的=
while (count <= input.length() ) {
and make it 并使其
while (count < input.length() ) {
because it is causing the substring to read beyond the length of the string. 因为它导致子字符串读取超出字符串长度。
=============== But I'll add a few extra bits of advice even though its not asked for: ==================但我会添加一些额外的建议,即使它并不需要:
do not use == to compare strings, use 不要使用==比较字符串,请使用
letter.equals("a")
instead. 代替。 Or even better, try using
甚至更好,请尝试使用
char c = input.charAt(count);
to get the current character then compare like this: 获取当前字符,然后像这样进行比较:
c == 'a'
Removing the equal sign should fix that. 删除等号应该可以解决该问题。
while (count < input.length()) {
and since you want to get a single character, you should do this: 并且由于要获得单个字符,因此应执行以下操作:
substr(count,1)
because the 2nd parameter is actually length, not index. 因为第二个参数实际上是长度,而不是索引。
I think your loop condition should be count < input.length
. 我认为您的循环条件应为
count < input.length
。 Right now, the last iteration runs with count == length
, so your substring
call is given a start index after the last character in the string, which is illegal. 现在,最后一次迭代以
count == length
运行,因此substring
调用在字符串中的最后一个字符之后被赋予起始索引,这是非法的。 These type of boundary errors are very common when writing such loops, so it's always good to double- and triple-check your loop conditions when you encounter a bug like this. 在编写此类循环时,这类边界错误非常常见,因此,当遇到此类错误时,最好对循环条件进行两次和三次检查。
Also, comparing strings with the ==
operator usually won't do what you want. 另外,将字符串与
==
运算符进行比较通常不会做您想要的事情。 That compares whether or not the two variables reference the same object. 比较两个变量是否引用相同的对象。 Instead, you want to test
string1.equals(string2)
, which compares the contents of the two strings. 相反,您要测试
string1.equals(string2)
,它比较两个字符串的内容。
Fixed it with help from everyone, and especially Vincent. 在所有人(尤其是文森特)的帮助下进行了修复。 Thank you!
谢谢! Runs wonderfully.
运行出色。
import java.util.Scanner;
public class VowelCount {
public static void main(String[] args) {
int a = 0, e = 0, i = 0, o = 0, u = 0, count = 0;
String input;
char letter;
Scanner scan = new Scanner (System.in);
System.out.print ("Please enter a string: ");
input = scan.nextLine();
while (count < input.length() ) {
letter = input.charAt (count);
if (letter == 'a')
a++;
if (letter == 'e')
e++;
if (letter == 'i')
i++;
if (letter == 'o')
o++;
if (letter == 'u')
u++;
count++;
}
System.out.println ("There are " + a + " a's.");
System.out.println ("There are " + e + " e's.");
System.out.println ("There are " + i + " i's.");
System.out.println ("There are " + o + " o's.");
System.out.println ("There are " + u + " u's.");
}
}
Before loop,try below 循环前,请尝试以下
if(input.length()>0){
//you code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.