Displaying and calculating 2D arrays

Discussion in 'Java' started by JSTBO, Mar 6, 2011.

  1. JSTBO

    JSTBO New Member

    Joined:
    Mar 6, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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.

    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.

    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 very new at Java and any help would be greatly appreciated!!!!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice