Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27
  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: I need help with my 2D Array

    From post#17
    You need a variable that holds the value of the next row in the array where the next session's data is to be stored.
    The variable needs to be defined where it's value will be preserved between calls to the listener method. The listener method will use it to access the row in the array and increment it to the next row for the next call to the listener method.

    One way to preserve the variable's value is to define it as a class variable.

    i is a POOR name for the variable.
    The variable holds the next session row's value. Use a name that says that.

    java.lang.ArrayIndexOutOfBoundsException: 5
    You have to stop the user from entering more than 5 values. The code could test the value of the index to the next row and not use it or change it if its value is past the end of the array. Or the code could disable the button at 5 sessions so the user can't press it again.
    If you don't understand my response, don't ignore it, ask a question.

  2. #22
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: I need help with my 2D Array

    Quote Originally Posted by dalythe View Post
    Java Code:
    int i = 0;
    data[i][0] = (double)session;
    data[i][1] = (double)earnings;
    i++;
    java.lang.ArrayIndexOutOfBoundsException: 5
    i think your for loop is a problem please refer this link:Java: Arrays -- 2-dimensional

    i hope this will be help to you
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  3. #23
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my 2D Array

    tamilarasi, thanks for the input but I eliminated the first for loop because I couldn't use it to populate the array. The second loop is working fine now. My main problem now is populating the 2D array. I was given the suggestion of creating instance variables for each location in the array. I guess I'm not grasping the Listener method properly, which is why I'm researching on it now. I just don't understand how the listener method would know that after I press the button move to the next row in the array. Because this is what I can't get to happen.

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: I need help with my 2D Array

    how the listener method would know that after I press the button move to the next row in the array
    The nextSessionRow variable would have the value of the index to the row in the array to use. After the listener method uses that slot, it should increment the nextSessionRow variable so it points to the next empty slot.
    If you don't understand my response, don't ignore it, ask a question.

  5. #25
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my 2D Array

    It's just not clicking with me. I don't mean to be a pest, I am just trying to understand. I have been doing Java for about 4 weeks now and I have been teaching myself online. It is just not incrementing. As a matter of fact, if I leave the code the way it is it reacts like a loop. I created instance variables for the rows, but couldn't see how to implement that in my actionPerformed method so I commented them out. Here is the code I currently have:

    Java Code:
    public class TutorEarnings extends javax.swing.JFrame {
    //Instance Variable
        private double sessionMinutes;
        private double sessionEarnings;
        private double totalmin;
        private double totalearn;
        private double avgwage;
        private double minwage;
        private double areaMessage;
    
        double data[][] = new double[5][2];
        //double rowValues = data[0][0];
        //double row1Values = data[1][0];
        //double row2Values = data[2][0];
        //double row3Values = data[3][0];
        //double row4Values = data[4][0];
        
     private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
            /** collect each session time and session earnings, assign them to
             * sessionMinutes and sessionEarnings variable, then store each entry into the array
             * tutor, and finally clear the Text Fields
             */
            try {
                String sessionT = tBox1.getText();
                String wage = tBox2.getText();
                
                double sessionMinutes = Double.parseDouble(sessionT);
                double sessionEarnings = Double.parseDouble(wage);
                            
                tBox1.setText("");
                tBox2.setText("");
                          
                data[0][0] = (double) sessionMinutes;
                data[0][1] = (double) sessionEarnings;
               
                data[1][0] = (double) sessionMinutes;
                data[1][1] = (double) sessionEarnings;
                
                data[2][0] = (double) sessionMinutes;
                data[2][1] = (double) sessionEarnings;
                
                data[3][0] = (double) sessionMinutes;
                data[3][1] = (double) sessionEarnings;
                
                data[4][0] = (double) sessionMinutes;
                data[4][1] = (double) sessionEarnings;
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Invalid input. Please try again.");
            }
            
    JOptionPane.showMessageDialog(null, "Session Time and Session Earning Entered.");
    
            
        }

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: I need help with my 2D Array

    You need to consider how the user enters data and how that data gets put into the array. There is a sort of loop that goes from user to the listener and then the user again and again the listener, etc

    Pseudo code:
    set nSR = 0; // index to the array
    begin loop to get session data from user
    user enters data
    user presses button
    listener gets control
    listener gets data user entered for one session
    listener saves data in array using nSR as index
    listener increments nSR
    end loop
    If you don't understand my response, don't ignore it, ask a question.

  7. #27
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: I need help with my 2D Array

    I totally GOT IT! Thanks. I finally figured out about the variable representing the row location in the array. After I got that straight, I was able to see how the listener incriminated through the array. Again, thanks Norm.

    Java Code:
    public class TutorEarnings extends javax.swing.JFrame{
    //Instance Variable
        private double sessionMinutes;
        private double sessionEarnings;
        private double totalmin;
        private double totalearn;
        private double avgwage;
        private double minwage;
        private double areaMessage;
    
        double data[][] = new double[5][2];
        int sessionRows = 0;
    
        String sessionT = tBox1.getText();
        String wage = tBox2.getText();
                
        double sessionMinutes = Double.parseDouble(sessionT);
        double sessionEarnings = Double.parseDouble(wage);
                
         //Store the variable values in the data array
         data[sessionRows][0] = sessionMinutes;
         data[sessionRows][1] = sessionEarnings;
         sessionRows++;
                
          //clear JTextFields            
          tBox1.setText("");
          tBox2.setText("");

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2013, 09:39 PM
  2. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  3. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 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
  •