简体   繁体   中英

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

    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]+"."); 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.

The increment expression is invoked after each iteration through the loop.

So when accessing 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).

If you take this snippet of code:

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

It outputs 2.

At that point variable i has incremented to 2 So you would have to reset it first. Ofcourse you get IOOB exception because you are referencing missing place in array (only places 0 and 1 exist)

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(2) { //ALWAYS
..........
..........
}

Hence, the switch(1) case, or the System.out.println(customer[i]+".") is never reached. It's quite a common mistake.

What you need is a do while loop for your menu.

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. So for example, in the do while , your default exit statement will always be printed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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