简体   繁体   English

我有一个 ArrayIndexOutOfBoundsException 但我不知道为什么

[英]I'm having an ArrayIndexOutOfBoundsException and I don't know why

I'm getting an ArrayIndexOutOfBoundsException, this is the full code我收到一个 ArrayIndexOutOfBoundsException,这是完整的代码

public static void main(String[] args){
    input();
}
static void input(){
    Scanner scan=new Scanner(System.in);
    System.out.println("Please enter the first name");
    String name1=scan.nextLine();
    System.out.println("Please enter the second name");
    String name2=scan.nextLine();

    boolean retval=name1.equalsIgnoreCase(name2);

    if(retval){
        String[] strs=name1.split(" ");

        for(int x=0;x<strs.length-1;x++){
            String con=Character.toString(strs[x].charAt(0));
            System.out.print(con+ " ");
        }
        System.out.print(strs[strs.length]);
    }
    else{
        input();
    }
}

So I'm making a code where you input two names and if they're equal(ignoring cases) the program will print the first and middle initials and then the surname.所以我正在制作一个代码,你输入两个名字,如果它们相等(忽略大小写),程序将打印第一个和中间的首字母,然后是姓氏。 I don't know why I'm getting this error since, as you can see, the value of variable x is definitely less than the length of the array.我不知道为什么会出现此错误,因为正如您所看到的,变量 x 的值肯定小于数组的长度。 For some reason this error appears every time.由于某种原因,这个错误每次都会出现。

run:
Please enter the first name
Liam Elijah Lucas
Please enter the second name
liam elijah lucas
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at Exercise3.input(Exercise3.java:34)
    at Exercise3.main(Exercise3.java:16)
L E Java Result: 1
BUILD SUCCESSFUL (total time: 15 seconds)

Apparently the last index after the loop is always one more than the max number of indexes in the strs array.显然,循环后的最后一个索引总是比 strs 数组中的最大索引数多一个。 If someone can answer and help it would be greatly appreciated.如果有人可以回答并提供帮助,将不胜感激。

strs[strs.length] does not exist If you type "liam iljiah lucas", then strs[strs.length]不存在 如果您输入“liam iljiah lucas”,则

strs[0].equalsIgnoreCase("liam") &&
strs[1].equalsIgnoreCase("elijah") &&
strs[2].equalsIgnoreCase("lucas")

strs[3] is missing缺少strs[3]

暂无
暂无

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

相关问题 我不知道为什么ArrayIndexOutOfBoundsException在单独链接中发生 - I don't know why ArrayIndexOutOfBoundsException occur in Separate Chaining Java程序中出现ArrayIndexOutOfBoundsException错误,我不知道如何解决 - ArrayIndexOutOfBoundsException error in Java program, and I don't know how to fix it ArrayIndexOutofBoundsException错误,但不知道为什么 - ArrayIndexOutofBoundsException Error but don't know why 调用getChoice()方法时出现错误,我不知道为什么 - I'm getting an error here when I call my getChoice() method, and I don't know why 我得到一个ArrayIndexOutOfBounds异常,我不知道为什么。 有人可以解释一下吗? - I'm getting an ArrayIndexOutOfBounds Exception and I don't know why. Can someone shed some light? 我遇到了 java.lang.NoClassDefFoundError 我不知道为什么会发生 - I'm encounter a java.lang.NoClassDefFoundError and I don't know why it's occuring 我应该得到“无效”作为输出,但我得到“有效”作为输出,不知道为什么输出是错误的 - I should be getting "invalid" as output but I'm getting "valid" as output, don't know why the output is wrong 我不知道为什么我只是通过创建一个新的 Clock 对象来得到一个错误 - I don't know why I'm getting an error simply by creating a new Clock object 我正在学习如何开发应用程序,但我不知道为什么我的应用程序崩溃了 - I'm learning how to develop an app and I don't know why my app is crashed 遇到异常,我不知道为什么 - Getting exception and I don't know why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM