简体   繁体   中英

ArrayIndexOutOfBoundsException when printing a line

Hey stackoverflow community been working at this program for a few days and been stuck on this error for awhile and can't get past it. Wondering if anyone can offer insight on what is going on. Thanks for all replies.

Here is the output when I run the program:

27050 
45200 
22600 
36250 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at program10.Tax.printBrackets(Program10.java:69)
at program10.Program10.main(Program10.java:16)

Below is some snippets of the source code. Under the main class is:

Tax tx = new Tax();
tx.setFilingStatus(tx.MARRIED_JOINTLY);
tx.setBrackets(2001);
tx.getBrackets();
tx.printBrackets();

Under the tax class I have the printBracket line that it is getting the error at:

public void printBrackets(){
    for (int i = 0; i < brackets.length; i++) {
        for (int j = 0; i < brackets[0].length; j++) {
            System.out.println(brackets[i][j] + " ");
        }
    }
}

Lastly is the 2001.brackets file it is pulling the information from:

20
27050 45200 22600 36250
65550 109250 54625 93650
136750 166500 83250 151650
297350 297350 148675 297350
2147483647 2147483647 2147483647 2147483647

Probably you should say

j < brackets[0].Length

instead of i in the inner for loop...

Nevermind, my printBrackets method was wrong can't believe I overlooked that for so long. Thanks anyways!

This line
for (int j = 0; i < brackets[0].length; j++)
should be
for (int j = 0; j < brackets[0].length; j++)

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