Results 1 to 6 of 6
  1. #1
    toamille is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Unhappy Calling data from an array to a text area

    I'm having trouble with my 2nd button. I need it to get data from an array with 2 columns. My loops/calculations include total time, total wages and average wage per hour. I can't seem to get data to show up in the text area correctly

    Java Code:
     public class task1ketUI extends javax.swing.JFrame {
    double[][] dataArray = new double [5][2];
    double i=0;
    double j=0;        
        /**
         * Creates new form task1ketUI
         */
        public task1ketUI() {
            initComponents();
        }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jOptionPane2 = new javax.swing.JOptionPane();
            jButton3 = new javax.swing.JButton();
            jPanel1 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jTextField2 = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jPanel2 = new javax.swing.JPanel();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
            jButton2 = new javax.swing.JButton();
    
            jOptionPane2.addContainerListener(new java.awt.event.ContainerAdapter() {
                public void componentAdded(java.awt.event.ContainerEvent evt) {
                    jOptionPane2ComponentAdded(evt);
                }
            });
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBackground(new java.awt.Color(153, 153, 255));
    
            jButton3.setText("Quit");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
    
            jPanel1.setBackground(new java.awt.Color(102, 102, 255));
            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Time and Wages", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 0)));
    
            jLabel1.setText("Time in Minutes");
    
            jLabel2.setText("Wages Earned");
    
            jTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField1ActionPerformed(evt);
                }
            });
    
            jTextField2.setText("0.00");
    
            jButton1.setText("Enter");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addGap(0, 0, Short.MAX_VALUE)
                            .addComponent(jButton1)
                            .addGap(19, 19, 19))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 109, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTextField1)
                                .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE))
                            .addGap(58, 58, 58))))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(3, 3, 3)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(32, 32, 32)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addGap(34, 34, 34))
            );
    
            jPanel2.setBackground(new java.awt.Color(102, 102, 255));
            jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Report Area", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 0)));
    
            jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            jScrollPane1.setViewportView(jTextArea1);
    
            jButton2.setText("Run Report");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jButton2)
                    .addContainerGap())
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap(25, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                            .addComponent(jButton2)
                            .addGap(144, 144, 144))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())))
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton3)
                            .addContainerGap(93, Short.MAX_VALUE))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton3)))
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    
        }                                           
    
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            System.exit(0);
        }                                        
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            double minutes = Double.parseDouble(jTextField1.getText());
            double wages = Double.parseDouble(jTextField2.getText());
    
            //Each time a user enters their time, we will add it to our array
            for (int row=5; row<dataArray.length; row++)
            {for (int col=2; col<dataArray.length; col++)
            {jTextArea1.setText(" " + dataArray[row][col]);} }
            
      
            
       // checks for valid data and gives a warning
    double jTextField1 = 0;
     
            try  {
                    jTextField1 = Double.parseDouble(
                            this.jTextField1.getText());
                if (jTextField1 <= 0 ){
                    throw new Exception();
                }
                 }
            catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Sorry. Minutes can't be less than 0.",
                        "Error", JOptionPane.ERROR_MESSAGE);
                return;
                 }
           
             try  {
                    jTextField1 = Double.parseDouble(
                            this.jTextField1.getText());
                if (jTextField1 > 240){
                    throw new Exception();
                }
                 }
            catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Sorry. Minutes can't be 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, "Sorry. Earnings must be greater than zero."
                       , "Error", JOptionPane.ERROR_MESSAGE);
                       return;
                }      
    // here it the out put to the textarea
            dataArray[0][0] = minutes;
            dataArray[0][1] = wages;        
            jTextArea1.append("\nTime in Minutes and The wages\n " + minutes + "              " + wages);
            
            
        }                                        
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       
    //starts the report
            jTextArea1.append("\nYour Report:\n");
          
    //add total minutes worked to display in textarea1
    
    int totalminutes = 0;
        for (int minutes = 0; minutes <= 5; minutes++)
    {
       totalminutes = (int) (totalminutes + (dataArray[minutes][0]));
       
            jTextArea1.append("Total Minutes= " + totalminutes );
           jTextArea1.append("\n"); //add a line break
    }	
            //add total wages and display in text area
    
      int totalwages = 0;      
            for (int wages = 0; wages<=5; wages++) 
            { 
                totalwages = (int) (totalwages + (dataArray[0][wages]));
                        
            jTextArea1.append("Total Earnings= " + totalwages );
            jTextArea1.append("\n"); //add a line break
            }
    
             //calculates average per hour wage
    
        int average = 0;
        {
          average = (totalwages / totalminutes /60);
            jTextArea1.append("Average Per Hour Wage= " + average);
           jTextArea1.append("\n"); //add a break between lines
        }
         //wage analysis based on average and current minimum wage
        if (average < 7.25)
            {
            jTextArea1.append("The average wage per hour are below minimun");
            }
           else if (average >= 7.25 && average <= 7.25*2)
            {
            jTextArea1.append("Your average wages per hour are then average");
            }
           else
         {
            jTextArea1.append("The average wage per hour are above minimun");
        }
        }                                        
    
        private void jOptionPane2ComponentAdded(java.awt.event.ContainerEvent evt) {                                            
            // TODO add your handling code here:
        }                                           
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /*
             * Set the Nimbus look and feel
             */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /*
             * If Nimbus (introduced in Java SE 6) is not available, stay with the
             * default look and feel. For details see
             * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(task1ketUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(task1ketUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(task1ketUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(task1ketUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new task1ketUI().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JOptionPane jOptionPane2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        // End of variables declaration                   
    }
    Last edited by toamille; 04-13-2012 at 03:27 PM.

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Calling data from an array to a text area

    Am I not getting it correctly or are you starting your loop at the end of the array?
    Imho this will never reach the setText() statement... ;)

    Java Code:
        double[][] dataArray = new double [5][2];
    
     ....
    
            for (int row=5; row<dataArray.length; row++)
            {for (int col=2; col<dataArray.length; col++)
            {jTextArea1.setText(" " + dataArray[row][col]);} }

  3. #3
    toamille is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default Re: Calling data from an array to a text area

    Thanks for the reply!!
    I dont know what that the questions means? The idea is user enters data in textfield1 and textfield2. that data gets put into a 2 d array which needs to be accessed by the 2nd button to run "the report" of all the calculations.

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Calling data from an array to a text area

    You have an array that is size 5x2 ... in your loop you start at row=5 and your loop condition is row<dataArray.length which is 5... as row=5 is equal not smaller than length=5 you will never even enter your loop whatever you do there.

    Correct:
    for (int row=0; row<dataArray.length; row++)
    Last edited by Sierra; 04-13-2012 at 03:52 PM.

  5. #5
    toamille is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default Re: Calling data from an array to a text area

    Thanks again.
    for (int row=0; row<dataArray.length; row++)
    {for (int col=2; col<dataArray.length; col++)
    {jTextArea1.setText(" " + dataArray[row][col]);} }
    With this correction, the enter button, button1 no longer loads the user entered data into the text area. New question, what do i need to fix so textfield1 and 2 write to the textarea?

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Calling data from an array to a text area

    Try to debug your function... the other error is the same as the first one just a row below...
    Well it is not the only error in that loop... take a look, try to find the other 2 - for the one I give you a hint:
    col<dataArray.length
    What is dataArray.length? 5
    What is the dimension of the array with respect to "col" (second dimension of the array!) at maximum? I guess 2, right?
    You need the dataArray[row].length here probably...

    Now try and fix your code - guess we cannot find you all the logical problems in your code... print out the values at certain locations using System.out.println() or use the step by step debugging of your dev environment. Start your code with simple functions and only procees if those functions work as expected instead of writing all at once.

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2011, 05:09 PM
  2. Replies: 0
    Last Post: 12-09-2010, 06:56 PM
  3. under line text area data
    By anilkumar_vist in forum New To Java
    Replies: 1
    Last Post: 02-25-2010, 04:37 PM
  4. Displaying data into text area
    By abhiN in forum New To Java
    Replies: 1
    Last Post: 01-22-2008, 11:30 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
  •