Losing data from array - not sure why
I'm using the line Code:
System.out.println(userInput[0][0] +"\t" + userInput [0][1]);
to watch the array as I work.
The first part where the user enters the data works fine. After the Enter button is clicked, the 2 values appear in the console window and they are correct. They're in the correct places.
Here's the Enter button code:
Code:
private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Get input (minutes and payment) from user
try {
minutesIn = Double.parseDouble(minutesField.getText());
paymentIn = Double.parseDouble(paymentField.getText());
if (minutesIn >= 0); else {
outputTextArea.append("Please enter a vaild number of minutes\n");
return;
}
if (paymentIn >= 0); else {
outputTextArea.append("Please enter a vaild payment amount\n");
return;
}
} catch (Exception e) {
outputTextArea.append("Please input a numerical value\n");
return;
}
// Store input into array
double[][] userInput = {{minutesIn,paymentIn}};
System.out.println(userInput[0][0] + "\t" + (userInput[0][1]));
//Print entries into textarea(outputTextArea)
if (i == 1) {
outputTextArea.append("*****************************\n");
outputTextArea.append("Raw Tutoring Earnings Data\n\n");
outputTextArea.append("Minutes" + "\t" + "Earnings\n");
outputTextArea.append(minutesIn + "\t" + paymentIn + "\n");
i++;
} else {
outputTextArea.append(minutesIn + "\t" + paymentIn + "\n");
}
totalMinutes += minutesIn;
totalPayment += paymentIn;
averageWage = (totalPayment / (totalMinutes / HOUR));
//add one to array row counter
row++;
Now I have a Report button where I'm going to do my calculations for the totals. That is going to replace the lines
Code:
totalMinutes += minutesIn;
totalPayment += paymentIn;
averageWage = (totalPayment / (totalMinutes / HOUR));
in the Enter button code.
Problem is when I see the values for the array after hitting the Report button, they are 0.0 0.0
Here's the Report button code:
Code:
private void reportButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Run report (calcualtion and print to screen)
//array work
//for(int counter=0;counter<userInput.length;counter++){
// totalMinutes+=userInput[counter][counter];
//}
System.out.println(userInput[0][0] +"\t" + userInput [0][1]);
outputTextArea.append("\n\n\n********************************\n");
outputTextArea.append("Report of your wages to Date:\n\n");
outputTextArea.append("Total minutes spent tutoring= " + totalMinutes + "\n");
outputTextArea.append("Total Earnings= $" + totalPayment + "\n");
outputTextArea.append(String.format("Average Per Hour Wage= $%.2f", averageWage));
outputTextArea.append("\n\n");
if (averageWage < minWage) {
outputTextArea.append("Your average wages per hour are below average");
} else if (averageWage >= minWage && averageWage <= minWage * 2) {
outputTextArea.append("Your average wages per hour are average");
} else if (averageWage > minWage * 2) {
outputTextArea.append("Your average wages per hour are above average");
}
}
(Disregard all the remmed out stuff. That's my trial and error work.)
Near the top I run that test line that prints out the array data. It comes back as all 0s.
I don't understand why the array data didn't carry over.
Should I just do the calculations for the totals in the Enter button code since the values are there?
Re: Losing data from array - not sure why
You look to be completely re-creating the array each time actionPerformed is called, and so it makes sense that nothing held previously is saved. You do appear to be incrementing the row variable, but don't appear to be using it anywhere. Shouldn't this be used as the array row index for the newly added data?
If you're dead set on using an array, then consider creating an array of adequately large size on object creation, perhaps in the constructor, and then only adding data to it, using the row variable, inside of the actionPerformed method. Alternatively, you could package your two data points in their own object that you've created and then add them to an ArrayList in the actionPerformed method. If you go this route, then again the ArrayList needs to be created on object creation, not inside of the actionPerformed method.
Re: Losing data from array - not sure why
So since recreating the array is the problem, all the calculations need to be done under the Enter button, I assume.
I had a feeling that the way I declared the array was a problem. All the books I have use a different style. I guess that means rethinking things a bit but it may help me because all the calculation examples I've been seeing use the regular type of declaration. It's got to be done in a 2D array. I've actually got the program working with no array (and it works perfectly), but that won't cut it.
I came up with this, doesn't work (yet) but it may be closer to what I need. This is the enter button code -
Code:
private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Get input (minutes and payment) from user
try {
minutesIn = Double.parseDouble(minutesField.getText());
paymentIn = Double.parseDouble(paymentField.getText());
if (minutesIn >= 0); else {
outputTextArea.append("Please enter a vaild number of minutes\n");
return;
}
if (paymentIn >= 0); else {
outputTextArea.append("Please enter a vaild payment amount\n");
return;
}
} catch (Exception e) {
outputTextArea.append("Please input a numerical value\n");
return;
}
// Store input into array
// original declaration below
//double[][] userInput = {{minutesIn, paymentIn}};
//double[][] userInput = new double[20][2];
//watch array
System.out.println(userInput[0][0] + "\t" + (userInput[0][1]));
//array calcs
double[][] userInput = new double[20][2];
for (int i = 0; i < userInput.length; i++) {
for (int j = 0; j < userInput[i].length; j++) {
userInput[i][j] = 2;
System.out.println(userInput[i][j]);
}
}
//Print entries into textarea(outputTextArea)
if (printHeader == 0) {
outputTextArea.append("*****************************\n");
outputTextArea.append("Raw Tutoring Earnings Data\n\n");
outputTextArea.append("Minutes" + "\t" + "Earnings\n");
outputTextArea.append(minutesIn + "\t" + paymentIn + "\n");
printHeader++;
} else {
outputTextArea.append(minutesIn + "\t" + paymentIn + "\n");
}
totalMinutes += minutesIn;
totalPayment += paymentIn;
averageWage = (totalPayment / (totalMinutes / HOUR));
//add one to array row counter
//row++;
//clear jTextFields (minutesField & paymentField)
minutesField.setText("");
paymentField.setText("");
}
Using the println to watch the array and it prints out 20 2.0s so there something wrong there.
I'm guessing the it's the Code:
userInput[i][j] = 2
Shouldn't the for loops cause the row to advance after input from the user (enter button clicked)?
Re: Losing data from array - not sure why
You appear to be re-creating your array in the handler code, just what I recommended you not do in my previous post. Why?
Re: Losing data from array - not sure why
Ooops, sorry. Didn't completely realize that.
OK, I remmed out that line (#31). Now my output in the console is a bit stranger.
After one click of the enter button, I get this in the console -
run:
0.0 0.0
2.0
2.0
Re: Losing data from array - not sure why
And get rid of the for loop. Your button is for entering one row of data only. You're incrementing the row variable, but again, you're not using it as I've recommended.
Re: Losing data from array - not sure why
]I have my array declared like this -
Code:
double[][] userInput = {{minutesIn, paymentIn}};
Should it be something like this?
Code:
double[][] userInput = new double[row][col];
Re: Losing data from array - not sure why
Use a variation of he latter, something like:
Code:
double[][] userInput = new double[MAX_ROWS][COLUMNS];
Where MAX_ROWS is a constant that should be adequate for all the rows your app will need, and COLUMNS is an int constant, 2. Don't declare this in the event handler but as a field of the class. If this were my app though, I'd create a class with two fields, one for minutesIn and one for paymentIn, and then I'd create an ArrayList of objects of this class rather than messing with partially filled arrays.
Re: Losing data from array - not sure why
Allrighty.
Now the output from the console looks normal in format but it's 0.0 0.0
I'm missing something here. The values have been parsed and I thought that was enough to get them in to the array but they're not making it.
Maybe - Code:
data.add(minutesIn, paymentIn);
But something needs to be imported for data (cannot find symbol error), can't find out what though.
After messing around a bit it seems like this may work
Code:
userInput [MAX_ROWS][COLUMNS] = minutesIn ;
Still not right though. The console explodes in errors.