Results 1 to 9 of 9
- 12-01-2012, 11:14 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
Losing data from array - not sure why
I'm using the line
to watch the array as I work.Java Code:System.out.println(userInput[0][0] +"\t" + userInput [0][1]);
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:
Now I have a Report button where I'm going to do my calculations for the totals. That is going to replace the linesJava 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++;
in the Enter button code.Java Code:totalMinutes += minutesIn; totalPayment += paymentIn; averageWage = (totalPayment / (totalMinutes / HOUR));
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:
(Disregard all the remmed out stuff. That's my trial and error work.)Java 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"); } }
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.
- 12-02-2012, 05:57 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
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 -
Using the println to watch the array and it prints out 20 2.0s so there something wrong there.Java 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(""); }
I'm guessing the it's theShouldn't the for loops cause the row to advance after input from the user (enter button clicked)?Java Code:userInput[i][j] = 2
-
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?
- 12-02-2012, 06:06 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
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.
- 12-02-2012, 06:19 AM #7
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
Re: Losing data from array - not sure why
]I have my array declared like this -
Should it be something like this?Java Code:double[][] userInput = {{minutesIn, paymentIn}};
Java Code:double[][] userInput = new double[row][col];
-
Re: Losing data from array - not sure why
Use a variation of he latter, something like:
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.Java Code:double[][] userInput = new double[MAX_ROWS][COLUMNS];
- 12-02-2012, 07:19 AM #9
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
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 -But something needs to be imported for data (cannot find symbol error), can't find out what though.Java Code:data.add(minutesIn, paymentIn);
After messing around a bit it seems like this may work
Still not right though. The console explodes in errors.Java Code:userInput [MAX_ROWS][COLUMNS] = minutesIn ;
Last edited by neveser; 12-02-2012 at 07:52 AM.
Similar Threads
-
JTabbedPane losing listener
By mendescholl in forum AWT / SwingReplies: 1Last Post: 02-03-2012, 03:01 AM -
Swing Applet losing dirty data
By ptreves in forum Java AppletsReplies: 1Last Post: 09-25-2011, 03:30 PM -
Losing Packets/Bytes using DataInputStream and socket
By ajordanneve in forum NetworkingReplies: 0Last Post: 03-24-2009, 05:22 AM -
Problems with JFrame losing the Design view
By chris@gaiag.net in forum NetBeansReplies: 7Last Post: 07-23-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks