简体   繁体   English

Java中使用charAt的“字符串索引超出范围”错误消​​息

[英]'String Index out of Range' Error Message in Java using charAt

Recently, while coding I came upon the following error: 最近,在编码时遇到了以下错误:

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)

The error appears when I add the line - firstLetter=userName.charAt(0); 添加行时出现错误firstLetter=userName.charAt(0); to the code and program displays the error message after the user enters all the values asked. 用户输入所有要求的值后,代码和程序中的会显示错误消息。 Before this line was entered, it all worked fine. 在输入此行之前,一切正常。

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;
                }

The idea of the line was to see if the name entered by the user begins with either the letter 's' or 'S'. 该行的目的是查看用户输入的名称是否以字母“ s”或“ S”开头。

All the variables have been declared, I just haven't included them for the sake of keeping this post a little succinct. 所有变量都已声明,为使本文简洁一些,我只是未将它们包括在内。

I think you are pressing enter key by mistake without giving a chance to enter any input and it forces username variable be empty. 我认为您是在错误地按Enter键而没有机会输入任何输入,并且这迫使用户名变量为空。 I reproduced this error like mentioned above.Sometime when you deal with scanner , it happens like that. 我如上所述重现了此错误。有时当您使用scanner ,会发生这种情况。

So in your code, check whether the username is null or not before doing any operation. 因此,在执行任何操作之前,请在代码中检查username是否为null。

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

You may want to use if to make sure that the next line really exists. 您可能要使用if要确保下一行确实存在。

Something like this: 像这样:

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

This most likely not a good fix for your problem, but based on the limited information we have it's difficult to suggest anything better. 这很可能不能很好地解决您的问题,但是基于有限的信息,我们很难提出更好的建议。

The real problem is most likely elsewhere in your logic. 真正的问题很可能是您逻辑中的其他地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java String-超出范围的字符串索引,charAt - java String - String index out of range , charAt Java charAt() 字符串索引超出范围 - Java charAt() String index out of range Java charAt()字符串索引超出范围:0 - Java charAt() String index out of range: 0 Java charAt()字符串索引超出范围:5 - Java charAt() String index out of range: 5 java中的字符串索引越界错误(charAt) - string index out of bounds error in java (charAt) charAt(0) 字符串索引超出范围:0 - charAt(0) String index out of range: 0 Java日志文件读取器,charAt()字符串索引超出范围 - Java Log File Reader, charAt() String index out of range Java字符串索引越界,CharAt问题 - Java string index out of bound , CharAt problem 使用variable.charAt()重新排列数字。 线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围: - Rearranging numbers using variable.charAt(). Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 线程“main”中的异常 java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:658) - Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:658)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM