【问题标题】:'String Index out of Range' Error Message in Java using charAt使用 charAt 的 Java 中的“字符串索引超出范围”错误消​​息
【发布时间】:2013-04-05 17:00:47
【问题描述】:

最近,在编码时,我遇到了以下错误:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: 
                     String index out of range: 0
at java.lang.String.charAt(String.java:686)
at TestAssign2.main(TestAssign2.java:119)

当我将行 - firstLetter=userName.charAt(0); 添加到代码中时出现错误,并且程序在用户输入所有询问的值后显示错误消息。在输入此行之前,一切正常。

while(uProgStart.equals(PROGSTART))
        {


            System.out.println("What is your name?");
            userName=sc.nextLine();
            junk = sc.nextLine();



            System.out.println("What is the year of your birth?");
            birthYear = sc.nextInt();
            junk = sc.nextLine();

            System.out.println("What is the date of your birth? (dd.mm)");
            birthDDMM = sc.nextDouble();
            junk = sc.nextLine();



            day=(int)birthDDMM;

            month=(int)Math.round(100*birthDDMM)%100;

                //Begins calculation of Dss Score
                if(birthYear%CYCLE_LENGTH!=SNAKE_YEAR)
                    {
                        DssScore=DssScore+1;
                    }

                if(month<SPRING_BEGINNING||month>SPRING_END)

                    {
                        DssScore=DssScore+2;
                    }

     firstLetter=userName.charAt(0);            
            if(firstLetter==(LOWER_S)||firstLetter==(UPPER_S))
                {
                    DssScore=DssScore+4;
                }

该行的想法是查看用户输入的名称是否以字母“s”或“S”开头。

所有变量都已声明,只是为了保持这篇文章的简洁,我没有包含它们。

【问题讨论】:

    标签: java exception char runtime-error indexoutofboundsexception


    【解决方案1】:

    我认为您在没有机会输入任何输入的情况下错误地按了 Enter 键,它会强制用户名变量为空。我重现了上面提到的这个错误。有时当你处理scanner时,它会发生这种情况。

    所以在你的代码中,在做任何操作之前检查username是否为空。

    【讨论】:

    • Ta UVM。我确保用户可以输入一个字符串(他们的名字)并且错误仍然存​​在。下面是程序输出的一个例子:**你叫什么名字?** Sam 你的出生年份是什么? 1994 你的出生日期是什么? (dd.mm) 26.11 线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:java.lang.String.charAt(String.java:686) 处的 0
    • 你能粘贴完整的代码吗?并尝试像这样更改序列: System.out.println("What is your name?");垃圾 = sc.nextLine();用户名=sc.nextLine();
    • 我发现了问题。字符串 userName 确实为空,而用户输入的名称存储在垃圾变量中(而不是我想要的 enter)。感谢您对愚蠢错误的建议。 @柯比
    【解决方案2】:

    在调用 charAt 之前调用 sc.nextLine() 分配 userName 可能会返回一个空字符串(例如,如果您扫描一个空行)。

    【讨论】:

    • 那么您是否建议将 charAt 行移到 userName=sc.nextLine(); 上方?
    • 好吧,OP 没有显示扫描仪是如何初始化的,但是 userName=sc.nextLine() 的重点是检索用户名,对吧?目前,我敢打赌 sc.nextLine() 返回一个空字符串,所以你没有得到你想要的用户名。例如,在文件中,如果您正在扫描换行符,则可能会发生这种情况。
    • 我的预感是对 sc.nextLine() 的调用在那里得到了一个空白标记,所以你没有读取你想要的值。
    • 您对 userName=sc.nextLine() 的权利。但是,这是程序运行时的样子:What is your name? Sam What is the year of your birth? 1994 What is the date of your birth? (dd.mm) 26.11 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:686) at TestAssign2.main(TestAssign2.java:148) 所以字符串用户名不应该为空。
    【解决方案3】:

    您可能需要使用if 来确保下一行确实存在。

    类似这样的:

    if(sc.hasNextLine()) {
       userName = sc.nextLine()
    } else {
      System.out.println("OMG... Where is my line?")
    }
    

    这很可能不能很好地解决您的问题,但根据我们掌握的有限信息,很难提出更好的建议。

    真正的问题很可能在你逻辑的其他地方。

    【讨论】:

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