简体   繁体   English

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException

[英]Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException

I'm just a newbie in java, so please help me, i think the problem is in the switch stament我只是一个java新手,所以请帮助我,我认为问题出在switch stament上

    String customer[]=new String[2];
    int old[]=new int[2];

    for(i=0; i<customer.length;i++){
        System.out.println("\nEnter information of customer#" +(i+1));
        System.out.print("Enter customer name"+(i+1)+":");
        customer[i]=data.readLine();
        System.out.print("Enter old reading of costumer#"+(i+1)+":");
        old[i]=Integer.parseInt(data.readLine());
                    }

            System.out.println("\n\nSample Menu");
        System.out.println("1. Display Transaction\n2.Pay Water Bill");
        System.out.print("Enter your choice:");
            choice=Integer.parseInt(data.readLine());

In this part the System.out.println(customer[i]+".");在这部分 System.out.println(customer[i]+"."); is not working不管用

    switch(choice){
        case 1:
            System.out.println("This is to display the transaction!");
                            System.out.println(customer[i]+"."); \
                   break;
        case 2:
                 System.out.println("This is to pay the water bill!");
                break;
        default:                                                        System.out.println("Exit`!");
            break;

            }

} }

} }

The problem is that when you exit your loop, the value of i is 2, not 1.问题是当你退出循环时, i的值是 2,而不是 1。

The increment expression is invoked after each iteration through the loop.增量表达式在循环的每次迭代后调用。

So when accessing System.out.println(customer[i]+".");所以当访问System.out.println(customer[i]+"."); you go out of bounds since the last element of your array is at index 1 (arrays are 0 base indexed).由于数组的最后一个元素位于索引 1(数组的基数索引为 0),因此您越界了。

If you take this snippet of code:如果你使用这段代码:

int i;
for(i = 0; i < 2; i++){}
System.out.print(i);

It outputs 2.它输出 2。

At that point variable i has incremented to 2 So you would have to reset it first.那时变量i已经增加到2所以你必须先重置它。 Ofcourse you get IOOB exception because you are referencing missing place in array (only places 0 and 1 exist)当然,您会遇到 IOOB 异常,因为您正在引用数组中缺少的位置(仅存在01

This is how your code works :这是您的代码的工作方式:

for(i=0; i<customer.length;i++){ 
   ............................
   ............................
}

Hence, i takes values :

i     is (i < customer.length)
0           YES
1           YES
2            NO  <LOOP BREAKS>

Now, when it comes to the switch statement, the following happens:现在,当涉及到 switch 语句时,会发生以下情况:

switch(2) { //ALWAYS
..........
..........
}

Hence, the switch(1) case, or the System.out.println(customer[i]+".") is never reached.因此,永远不会到达switch(1)情况或System.out.println(customer[i]+".") It's quite a common mistake.这是一个很常见的错误。

What you need is a do while loop for your menu.您需要的是菜单的 do while 循环。

So :所以 :

// Initialize Values
for(i=0; i<customer.length;i++){ 
   ............................
   ............................
}

// Loop through the Options

do {
    // ASK FOR USER INPUT AS YOU ARE DOING

    switch(choice) { //ALWAYS
    ..........
    ..........
    }

} while(choice != 1 || choice != 2);

The do while ensures that no matter what, your command will be executed for the menu, when it is given. do while确保无论如何,您的命令都将在给出菜单时执行。 So for example, in the do while , your default exit statement will always be printed.因此,例如,在do while ,您的default退出语句将始终打印。

暂无
暂无

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

相关问题 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 7 - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:6 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 6 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:2 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException:2 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:5 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException: - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:-1 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -1 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException 4 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException 4 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:3 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:8 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 8 线程“主”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM