【问题标题】:String index out of bounds? (Java, substring loop)字符串索引超出范围? (Java,子字符串循环)
【发布时间】:2010-12-06 21:25:35
【问题描述】:

我为 COSC 课程制作的这个程序编译不正确,我不断收到错误:

线程“main”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:2

在 java.lang.String.substring(String.java:1765) 在 VowelCount.main(VowelCount.java:13)

这是我的代码:

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.");
 }
}

据我所知,这应该有效,但为什么不呢?任何帮助都会很棒。谢谢!

【问题讨论】:

    标签: java substring while-loop


    【解决方案1】:

    你可能需要去掉一行中的=

    while (count <= input.length() ) {
    

    做起来

    while (count < input.length() ) {
    

    因为它导致子字符串读取超出字符串的长度。

    =============== 但我会添加一些额外的建议,即使它没有被要求:

    不要使用 == 来比较字符串,使用

    letter.equals("a")
    

    相反。或者更好的是,尝试使用

    char c = input.charAt(count);
    

    获取当前字符然后像这样比较:

    c == 'a'
    

    【讨论】:

      【解决方案2】:

      我认为你的循环条件应该是count &lt; input.length。现在,最后一次迭代使用count == length 运行,因此您的substring 调用在字符串中的最后一个字符之后被赋予了一个起始索引,这是非法的。这些类型的边界错误在编写此类循环时非常常见,因此当您遇到此类错误时,最好对循环条件进行两次和三次检查。

      此外,使用== 运算符比较字符串通常不会达到您想要的效果。比较两个变量是否引用同一个对象。相反,您想测试string1.equals(string2),它比较两个字符串的内容。

      【讨论】:

        【解决方案3】:

        删除等号应该可以解决这个问题。

        while (count &lt; input.length()) {

        既然你想得到一个字符,你应该这样做:

        substr(count,1)

        因为第二个参数实际上是长度,而不是索引。

        【讨论】:

        • 好的,它编译!但仍然没有输出正确的数量。测试字符串 "aeiou" 结果为 0, 0, 0, 0, 0..
        • 不要使用 == 来比较其他答案中提到的字符串
        【解决方案4】:

        在所有人的帮助下修复了它,尤其是文森特。谢谢!运行非常好。

        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.");
            }
        }
        

        【讨论】:

        • 你可以使用 for 循环来进一步整理它: - for(int count = 0; count
        • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post
        • @WilQu 你读过这个问题吗?这是发布工作解决方案的OP。这应该怎么评论?
        • @meda 我将此视为评论,仅表示其他答案有效。它不会向现有答案添加任何内容。
        【解决方案5】:

        在循环之前,试试下面

        if(input.length()>0){
        //you code
        }
        

        【讨论】:

          猜你喜欢
          • 2010-10-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-31
          相关资源
          最近更新 更多