Results 1 to 6 of 6
  1. #1
    JSTBO is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default 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.

    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");
    
        }
        }
    I'm very new at Java and any help/suggestions would be greatly appreciated!!!!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    First of all you want to name your variables better as this is somewhat schizophrenic:

    Java Code:
    double jTextField1 = 0;
    
            try  {
                    jTextField1 = Double.parseDouble(
                            this.jTextField1.getText());
    What is jTextField1, the double variable or the JTextField?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    JSTBO is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default 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);

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JSTBO View Post
    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:
    Java Code:
    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
    Sure, it prints the array data out to the console, but has no code to display the data itself to the JTextArea.


    and this would be the code to print out the calculations:

    Java Code:
    jTextArea1.append("Total Minutes Spent Tutoring =" + totalmin);
    This will display something to the JTextArea, correct.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. need help with calculating something
    By mikec420 in forum New To Java
    Replies: 13
    Last Post: 09-29-2011, 09:14 PM
  2. calculating the hypotenuse
    By Latanyar in forum New To Java
    Replies: 6
    Last Post: 10-12-2010, 09:20 AM
  3. Help in calculating time different!
    By rocky86 in forum New To Java
    Replies: 6
    Last Post: 10-11-2010, 04:18 PM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  5. calculating exponents
    By GPB in forum New To Java
    Replies: 2
    Last Post: 03-21-2010, 11:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •