Java Programming Joyce Farrell, 8th editon exercise 10 pg 486, I need help creat
ID: 3866072 • Letter: J
Question
Java Programming Joyce Farrell, 8th editon exercise 10 pg 486, I need help creating a Gui for the program it works but error keeps getting thrown, Here is my code, and Output, I need to stop the execption error from appearing:
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 = 6;
static int reviews = 4;
private void 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]);
}
Explanation / Answer
You have the following code in your question:
else if(weeks < 0){
for (int i = 0; i < bonus[weeks].length; i++) {
}
}
--------------
After your edit in question:
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]);
}
Error is occuring during access to bonus[weeks][reviews].
You can correct the code in following manner:
int flag = 1; // everything is correct
weeks = Integer.parseInt(weeksWorked.getText());
if (weeks > 6) {
weeks = 6;
}
else if(weeks < 0){
flag = -1; // invalid number
}
reviews = Integer.parseInt(weeksWorked.getText());
if (reviews > 4) {
reviews = 4;
}
else if (reviews < 0){
flag = -1; // invalid number
}
if (flag == 1){
this.outputBox.setText("$" + bonus[weeks][reviews]);
}
else{
this.outputBox.setText("Invalid Number");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.