简体   繁体   English

StringIndexOutOFBoundsException:0

[英]StringIndexOutOFBoundsException: 0

I am a relative beginner to Java and am having an issue with a user controlled do-while loop that accepts user input to repeat. 我是Java的初学者,并且在用户控制的do-while循环中遇到问题,该循环接受用户输入以重复执行。 Here is the code at the end of the loop. 这是循环末尾的代码。

System.out.print("Do you wish to continue? (Y for yes " +
                            "/ N for no)");
        input = keyboard.nextLine();
        input.length();
        repeat = input.charAt(0);

        }while (repeat == 'Y' | repeat == 'y');

I know it's throwing the exception because of the value, but can't figure out what to do to fix it, as I'm sure it's relatively simple. 我知道它会因为值而抛出异常,但是由于要确保它相对简单,所以无法弄清楚该如何解决。 Thanks for the help. 谢谢您的帮助。

Edit 1: Stacktrace: 编辑1:Stacktrace:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: 
String index out of range: 0 
at java.lang.String.charAt(Unknown Source) 
at FractionTest.main(FractionTest.java:59)

It looks like you read empty line which returned you empty string "" so there is no characters there (not even 0th). 看起来您读了空行,并返回了空字符串""因此那里没有字符(甚至没有第0个字符)。

It usually happens when you are using nextLine right after other nextABC methods like nextInt since such methods doesn't consume line separators, and nextLine reads text until next line separator (or end of stream). 通常会在nextLine其他nextABC方法(例如nextInt之后使用nextLine时发生,因为此类方法不占用行分隔符,并且nextLine会读取文本,直到下一个行分隔符(或流的末尾)为止。

In that case you can add nextLine after that nextInt to consume line separator. 在这种情况下,您可以在nextInt之后添加nextLine以使用行分隔符。

Anyway to avoid reading character from empty string and exception you can use something like 无论如何要避免从空字符串和异常中读取字符,您可以使用类似

} while (input.toLowerCase().startsWith("y"));

instead of 代替

    input.length();//this actually doesn't change anything, you can remove it
    repeat = input.charAt(0);

} while (repeat == 'Y' | repeat == 'y');

As @Phesmo mentioned you may be getting a 0-length line. 正如@Phesmo所提到的,您可能会得到长度为0的行。 This is an obscure problem. 这是一个晦涩的问题。 Have you called nextInt() , nextLong() , nextShort() , nextByte() , or nextBoolean() on the Scanner before calling nextLine() ? 在调用nextLine()之前,您是否在Scanner上调用了nextInt()nextLong()nextShort()nextByte()nextBoolean() nextLine()


Try this 尝试这个

System.out.print("Do you wish to continue? (Y for yes / N for no)");

char ans;
do {
   ans = keyboard.next().charAt(0);
   // filter input
} while (ans == 'Y' || ans == 'y' || ans == 'N' || ans == 'n');

repeat = ans;

} while (repeat == 'Y' | repeat == 'y');

From documentation : 文档

String Scanner#next()
Finds and returns the next complete token from this scanner. 查找并返回此扫描仪的下一个完整令牌。 A complete token is preceded and followed by input that matches the delimiter pattern. 完整的标记在其前面,然后是与定界符模式匹配的输入。

Or this 或这个

System.out.print("Do you wish to continue? (Y for yes / N for no)");    
repeat = keyboard.next("[YyNn]").charAt(0);

} while (repeat == 'Y' | repeat == 'y');

From documentation : 文档

String Scanner#next(String pattern)
Returns the next token if it matches the pattern constructed from the specified string. 如果它匹配从指定字符串构造的模式,则返回下一个标记。

"[YyNn]" is a character class pattern, it means match any character from the set . "[YyNn]"是字符类模式,表示匹配集合中的任何字符

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM