简体   繁体   English

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

[英]Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5

What is wrong with my code (Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5)........我的代码有什么问题(线程“main”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:5)........

public class AAExample {
    public static void main(String[] args) {
        AAExample nn = new AAExample();
        System.out.println(nn.isXOrZ("Pony"));
    }

    public  boolean isXOrZ(String text) {
    String  lower = text.toLowerCase();
    boolean found = false;
    int     i     = 0;

    while (!found) {
        String letter = lower.substring(i, i +1);

            if(letter.equals("z") || letter.equals("x"))
            found = true;

            i++;
        }

        return found;
    }
}

Your while loop keep looping until x or z is found.您的 while 循环不断循环,直到找到xz Infinite loop occur when there is actually no x or z in your String .当您的String实际上没有xz时,就会发生无限循环。

StringIndexOutOfBoundsException occur when i >= lower.length()-1 StringIndexOutOfBoundsExceptioni >= lower.length()-1

So you need to modify your while loop to所以你需要修改你的while循环

 while (!found && i < lower.length()-1)

Your loop terminating condition only relies on whether character is found or not.您的循环终止条件仅取决于是否找到字符。

while (!found)

Suppose, you don't found anything then you will never do假设,你没有发现任何东西,那么你永远不会做

if(letter.equals("z") || letter.equals("x"))
   found=true

and keep on incrementing i , which will give you StringIndexOutOfBoundsException once it will be equals to string's length并继续增加i ,一旦它等于字符串的长度,它就会给你 StringIndexOutOfBoundsException

You are looping through the string until x or z is found.您正在遍历字符串,直到找到 x 或 z。 If x or z is not found, the loop will continue on past the end of the string.如果未找到 x 或 z,则循环将继续超过字符串的末尾。 This causes the exception.这会导致异常。

I would recommend a for loop:我会推荐一个 for 循环:

for(int i = 0; i < lower.length; i++) {
    String letter = lower.substring(i, i + 1);
    if(letter.equals("x") || letter.equals("z")) return true;
} 
return false;

暂无
暂无

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

相关问题 编译Java项目后出错:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - Error after compiling Java project : Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0 线程“main”中的回文字符串异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 - Palindrome String Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 Java:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围: - Java : Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 如何修复 Java 中的“线程“主”中的“异常”java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:5”问题 - How to fix “Exception in thread ”main“ java.lang.StringIndexOutOfBoundsException: String index out of range: 5” problem in Java 数字输入异常线程“main”java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - Number input Exception thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0 该程序不断给我一个错误:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:4 - This program keeps on giving me an error: Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 4 使用递归错误。 线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - using recursion error. Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 线程“ main java.lang.StringIndexOutOfBoundsException中的异常:字符串索引超出范围 - Exception in thread "main java.lang.StringIndexOutOfBoundsException: String index out of range 线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:4 - Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 4 线程“main”中的错误异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 - Error Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: -1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM