字符串索引超出范围?(Java,子字符串循环)
我为COSC课程制作的程序编译不正确,我不断收到错误: 线程“main”java.lang.StringIndexOutOfBoundsException中出现异常:字符串索引超出范围:2 位于java.lang.String.substring(String.java:1765) 在vouelcount.main(vouelcount.java:13) 这是我的密码:字符串索引超出范围?(Java,子字符串循环),java,substring,while-loop,Java,Substring,While Loop,我为COSC课程制作的程序编译不正确,我不断收到错误: 线程“main”java.lang.StringIndexOutOfBoundsException中出现异常:字符串索引超出范围:2 位于java.lang.String.substring(String.java:1765) 在vouelcount.main(vouelcount.java:13) 这是我的密码: import java.util.Scanner; public class VowelCount { public st
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.");
}
}
import java.util.Scanner;
公共类元音计数{
公共静态void main(字符串[]args){
int a=0,e=0,i=0,o=0,u=0,count=0;
字符串输入,字母;
扫描仪扫描=新扫描仪(System.in);
System.out.println(“请输入字符串:”);
输入=scan.nextLine();
而(count您可能需要取出行中的=
while (count <= input.length() ) {
相反,或者更好的是,尝试使用
char c = input.charAt(count);
要获取当前字符,请进行如下比较:
c == 'a'
去掉等号应该可以解决这个问题
while(count
由于您希望获得单个字符,因此应执行以下操作:
substr(计数,1)
因为第二个参数实际上是长度,而不是索引。我认为循环条件应该是count
。现在,最后一次迭代使用count==length
运行,因此子字符串
调用在字符串中最后一个字符之后有一个开始索引,这是非法的。这些类型的边界在编写这样的循环时,错误非常常见,所以当遇到这样的错误时,最好对循环条件进行两次或三次检查
此外,将字符串与=
运算符进行比较通常不会达到您的目的。这将比较两个变量是否引用同一对象。相反,您需要测试string1.equals(string2)
,它比较了两个字符串的内容。在大家的帮助下修复了它,特别是Vincent。谢谢!运行非常出色
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.");
}
}
import java.util.Scanner;
公共类元音计数{
公共静态void main(字符串[]args){
int a=0,e=0,i=0,o=0,u=0,count=0;
字符串输入;
字符字母;
扫描仪扫描=新扫描仪(System.in);
System.out.print(“请输入字符串:”);
输入=scan.nextLine();
while(count
在循环之前,请尝试以下操作
if(input.length()>0){
//you code
}
好的,它可以编译!但是仍然没有输出正确的数量。测试字符串“aeiou”会产生0,0,0,0,0..不要使用==来比较其他答案中提到的字符串。您可以使用for循环来进一步整理:-for(int count=0;countimport 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.");
}
}
if(input.length()>0){
//you code
}