-3

My program is suppose to calculate the bonus using a multidimensional array, based the weeks and reviews, I try to make the program fail, it returns the right string, but in the output box, tells me exception, How do I stop the exception within a multidimensional Array? Below is the code and Output.

static int bonus[][] = {{5, 9, 16, 22, 44},
    {10, 12, 18, 25, 36},
    {20, 25, 32, 42, 53},
    {32, 38, 45, 55, 68},
    {46, 54, 65, 77, 90},
    {60, 72, 84, 96, 120},
    {85, 100, 120, 140, 175}};

    static int weeks;
    static int reviews;

    calcButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
            weeks = Integer.parseInt(weeksWorked.getText());
            if (weeks > 6) {
                weeks = 6;
            }
            else if(weeks < 0){
                outputBox.setText("Invalid Number");
            }
            reviews = Integer.parseInt(weeksWorked.getText());

            if (reviews > 4) {
                reviews = 4;
            }
            this.outputBox.setText("$" + bonus[weeks][reviews]);
        }                                          

Output

Josh Adams
  • 17
  • 5

2 Answers2

0

value of variable is more than the array size. Print both array size and the review or week variable and check why its out of bound.

Amit Mahajan
  • 895
  • 6
  • 34
0

When you enter -1 you first set the outputBox text to "Invalid Number", but then you continue to use that invalid week number on line 123.

This would probably be easy to test if you used the debugger.

Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
  • How do I stop the outputBox from using it on the last line, I've never used the debugger? – Josh Adams Aug 03 '17 at 21:20
  • You could use an 'if else' so if input is bad set error text else do the wanted actions. (With actual code attached it would be easier to show) – Roger Lindsjö Aug 04 '17 at 05:36
  • In the menu you have debug options. One is to run you program in debug mode, another is to attach the debugget to a running java program. At the left margin of yor code where the line numbers are you can add breakpoints (shown as red dots). Once the execution reaches the breakpoint the program pauses and you can see the value of variables etc. you can also choose to continue running the program or just continue one line at a time. – Roger Lindsjö Aug 04 '17 at 05:40