Results 1 to 6 of 6
- 03-06-2011, 10:16 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Displaying and calculating 2D arrays
I need some help with a Netbeans GUI with 3 buttons and 1 text area. First button will
collect data from text field a and b and this is stored in a 2D array.
The second button will process 3 caluclations from the array data. The third button works fine.
The text area will display the data in the array when the first button is clicked and then
display the calculations when the 2nd button is clicked.
My first button (jbutton1) has the code for the exceptions and declaring the 2d array.
My issue here is that when I click the button it should display the data in the text area
but it doesn't. I see my string field but not the data.
Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // exception for minutes entered less than or equal to 0 and not greater than 240 double jTextField1 = 0; try { jTextField1 = Double.parseDouble( this.jTextField1.getText()); if (jTextField1 <= 0 || jTextField1 > 240){ throw new Exception(); } } catch (Exception e) { JOptionPane.showMessageDialog(this, "Invalid input. Minutes can't be less than 0 or more than 240. Please try again", "Error", JOptionPane.ERROR_MESSAGE); return; } double jTextField2 = 0; try { jTextField2 = Double.parseDouble( this.jTextField2.getText()); if (jTextField2 <=0) { throw new Exception(); } } catch (Exception e) { JOptionPane.showMessageDialog(this, "Invalid input. Earnings must be greater than zero. Please try again" , "Error", JOptionPane.ERROR_MESSAGE); return; } double[][] earnings = new double[10][2]; for (int i = 0; i <earnings.length; i++){ for (int j = 0; j < earnings[i].length; j++){ earnings[i][j]=2; //example System.out.println(earnings[i][j]); } } jTextArea1.append("\n Minutes Earnings\n *************************"); // separates detail from report and lists all minutes and earnings in array }
2nd issue is the 2nd button (jbutton2) should complete the calculations and display them in
the text area but all it shows is my string field. There is an issue with a NullPointerException
but I'm not sure how to correct this without creating additional errors in my calculations
code.
I'm very new at Java and any help/suggestions would be greatly appreciated!!!!Java Code:private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: JButton2 button2 = new JButton2("Click me"); //Displays in text area report title jTextArea1.append("Report of your wages to Date:"); jTextArea1.append("\n"); //start a new line //add total minutes tutoring and display in text area int totalmin = 0; int[] data = null; for (int jTextField1 : data) { totalmin = totalmin + jTextField1; jTextArea1.append("Total Minutes Spent Tutoring =" + totalmin); } //add total earnings and display in text area int totalearn = 0; for (int jTextField2 : data) { totalearn = totalearn + jTextField2; jTextArea1.append("Total Earnings= " + totalearn); } //calculates average per hour wage int average = 0; { average = totalearn / (totalmin / 60); jTextArea1.append("Average Per Hour Wage= " + average); jTextArea1.append("%\n"); //add a break between lines //notes in text area the minimum wage jTextArea1.append("Minimum Wage is currently = $6.55"); } //wage analysis based on average and current minimum wage if (average < 6.55) { jTextArea1.append("Your average wages per hour are below average"); } else if (average >= 6.55 && average <= 6.55*2) { jTextArea1.append("Your average wages per hour are then average"); } else { jTextArea1.append("Your average wages per hour are above average"); } }
-
First of all you want to name your variables better as this is somewhat schizophrenic:
What is jTextField1, the double variable or the JTextField?Java Code:double jTextField1 = 0; try { jTextField1 = Double.parseDouble( this.jTextField1.getText());
-
Next, where do you have code that tells Java to display the data itself in the JTextArea? I don't see any code posted above that does this, and if you don't tell Java to do this, it won't. The program does what you tell it to, no more and no less.
- 03-06-2011, 11:41 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Displaying and calculating 2D array
Thank you Fubarable for your reply. First, the variable for jTextField1 was created when I created the GUI in Netbeans. In the program, it noted that I was unable to change the variable name, but I looked again and I think when I set up the GUI, it allows me to put in a variable name, so I will change this. The jTextField1 is for the minutes or the [i] in the 2D array.
You noted there was no code to write the text in the jTextArea, but I thought the below code would do this. Again, I'm so very new to Java (an infant really) that I thought I had the code correct. If this is incorrect, how would I set up the code for Java to display the 2D array and calculations in the jTextArea?
I thought this would be the code to print out the 2D array:
earnings[i][j]=2; //example
System.out.println(earnings[i][j]);
}
}
jTextArea1.append("\n Minutes Earnings\n *************************");
// separates detail from report and lists all minutes and earnings in array
and this would be the code to print out the calculations:
jTextArea1.append("Total Minutes Spent Tutoring =" + totalmin);
-
Sure, it prints the array data out to the console, but has no code to display the data itself to the JTextArea.
This will display something to the JTextArea, correct.and this would be the code to print out the calculations:
Java Code:jTextArea1.append("Total Minutes Spent Tutoring =" + totalmin);
-
btw, when posting code here don't use color tags, use code tags so your code is readable.
Similar Threads
-
need help with calculating something
By mikec420 in forum New To JavaReplies: 13Last Post: 09-29-2011, 09:14 PM -
calculating the hypotenuse
By Latanyar in forum New To JavaReplies: 6Last Post: 10-12-2010, 09:20 AM -
Help in calculating time different!
By rocky86 in forum New To JavaReplies: 6Last Post: 10-11-2010, 04:18 PM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
calculating exponents
By GPB in forum New To JavaReplies: 2Last Post: 03-21-2010, 11:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks