Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Populating a 2D array with textfield values

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Populating a 2D array with textfield values

    I have to code an assignment for a college course. Long story short, part of the assignment requires me to take information from two textfields (variable names payment and time) and then enter them into a multidimensional array every time someone uses the enter button on a GUI. Then, after entering in all their payment and time amounts the user will press the "Run Report" button which will take information from the 2D array and list the information followed by some calculations like average, totals for time and payment, etc. I'm pretty much good to go on everything I think except this 2D array thing. The GUI is built and I'm trying to build functionality for the "Enter" button. I'm not real sure how to properly implement this though. Here's what I have so far.

    I first declared some global variables and the array:

     
    public class TutorUI extends javax.swing.JFrame {
     
    double[][] ptArray = new double[20][2];
    double i = 0;
    double j = 0;

    Then added some coding for the enter button:

    private void enterActionPerformed(java.awt.event.ActionEvent evt) {
     
    /*gets the value from the two text fields and sets them equal to the
    variables*/
    double paymentValue = Double.parseDouble(payment.getText());
     
    double timeValue = Double.parseDouble(time.getText());
     
    //Checks for negative numbers and throws exception if found.
    if (paymentValue < 0)
    {
    IllegalArgumentException exception
    = new IllegalArgumentException("Invalid payment amount.");
    throw exception;
     
    }
     
    //Checks for negative numbers and throws exception if found.
    if (timeValue < 0)
    {
    IllegalArgumentException exception
    = new IllegalArgumentException("Invalid time amount.");
    throw exception;
    }
     
    //Sets the variables to column 1 and column 2.
    ptArray[0][0] = timeValue;
    ptArray[0][1] = paymentValue;

    Basically I want to set the timeValue to column 1 and paymentValue to column 2 and then the array just adds a new row each time someone enters new data. I feel like maybe I'm going about this in the wrong way since when I try to test the array by appending the data to the textarea it spits out gibberish. I've tried about 20 different ways to get this to work and this is just one of the many. Any help, tips, links to tutorials, etc. will be greatly appreciated. Thanks.
    Last edited by drixnak; October 19th, 2010 at 11:26 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Populating a 2D array with textfield values

    Do you have to use a 2D array? It seems to me like a single List containing Objects that in turn contain time and payment information would be a lot easier to keep in your head. Plus, dealing with an unknown number of Objects in a List is a lot simpler than dealing with an unknown number of Objects in an array.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Populating a 2D array with textfield values

    Actually, people have asked that in the course, but they specifically want this 2D array.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Populating a 2D array with textfield values

    Well, in that case, I'd suggest you provide an SSCCE that demonstrates what you've got so far. The code you posted so far is missing a few key details:

    How are you appending the array data to the TextArea? What gibberish does it display?
    How are you keeping track of how many data pairs have been entered into the array?

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Populating a 2D array with textfield values

    I got an answer from someone on another forum regarding this and I was able to make it display what was needed. As far as keeping track of what was put into the array...we're not. The enter button just enters it into the array and that's it. The runReport button then iterates through the array listing the contents and then runs calculations, all of which are then displayed in the textarea. It's a really poorly worded assignment and it seems to go to great lengths to be overly complicated without being logical. I'll post the rest of the code in a few minutes once I get stuck again.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Populating a 2D array with textfield values

    Sigh. You might also want to post a link to your crosspost so we can avoid duplicating answers and wasting our time.

  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Populating a 2D array with textfield values

    Well it's a totally different forum and it's not a tech forum. Here's the code I have so far. The for loop in the Enter button action area is for testing purposes right now to see if my array was working correctly. It will be removed and moved to the runReport button functionality where it will list everything in the array. Then I'll add in some strings and some calculations like averages and totals. Thanks for the responses so far and sorry if I made you waste your time.

     
    package tutorcalculation;
     
    /**
     *
     * @author sleitzi
     */
    public class TutorUI extends javax.swing.JFrame {
     
        double[][] ptArray = new double[20][2];
        int i = 0;
     
        /** Creates new form TutorUI */
        public TutorUI() {
            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")
     
     
        private void quitActionPerformed(java.awt.event.ActionEvent evt) {                                     
            System.exit(0);
        }                                    
     
        private void enterActionPerformed(java.awt.event.ActionEvent evt) {                                      
     
            /*gets the value from the two text fields and sets them equal to the
             variables*/
            double paymentValue = Double.parseDouble(payment.getText());
     
            double timeValue = Double.parseDouble(time.getText());
     
            int index = 0;
            //Checks for negative numbers and throws exception if found.
            if (paymentValue < 0)
                {
                    IllegalArgumentException exception
                    = new IllegalArgumentException("Invalid payment amount.");
                    throw exception;
     
            }
     
            //Checks for negative numbers and throws exception if found.
            if (timeValue < 0)
            {
                IllegalArgumentException exception
                = new IllegalArgumentException("Invalid time amount.");
                throw exception;
            }
    //Sets the variables to column 1 and colum 2.
            ptArray[index][0] = timeValue;
            ptArray[index][1] = paymentValue;
            index++;
     
            for (int i = 0; i < index; i++){
                reportArea.append("Time: " + ptArray[i][0] + "Payment: " + ptArray[i][1]);
            }
     
     
        }                                     
     
        private void runReportActionPerformed(java.awt.event.ActionEvent evt) {                                          
     
     
        }                                         
     
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TutorUI().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JButton enter;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextField payment;
        private javax.swing.JButton quit;
        private javax.swing.JTextArea reportArea;
        private javax.swing.JButton runReport;
        private javax.swing.JTextField time;
        // End of variables declaration                   
     
    }

  8. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Populating a 2D array with textfield values

    Ok, quick question:

    private void enterActionPerformed(java.awt.event.ActionEvent evt) {                                      
     
            /*gets the value from the two text fields and sets them equal to the
             variables*/
            double paymentValue = Double.parseDouble(payment.getText());
     
            double timeValue = Double.parseDouble(time.getText());
     
            int index = 0;
            //Checks for negative numbers and throws exception if found.
            if (paymentValue < 0)
                {
                    IllegalArgumentException exception
                    = new IllegalArgumentException("Invalid payment amount.");
                    throw exception;
     
            }
     
            //Checks for negative numbers and throws exception if found.
            if (timeValue < 0)
            {
                IllegalArgumentException exception
                = new IllegalArgumentException("Invalid time amount.");
                throw exception;
            }
    //Sets the variables to column 1 and colum 2.
            ptArray[index][0] = timeValue;
            ptArray[index][1] = paymentValue;
            index++;
     
     
     
        }

    This should just enter it into the array and store it in memory correct?

    And then I want to append the list of information from the array to the textarea after I click the runReport button:

    private void runReportActionPerformed(java.awt.event.ActionEvent evt) {                                          
           int index = 0;
     
           for (int i = 0; i < index; i++){
                reportArea.append("Time: " + ptArray[i][0] + "Payment: " + ptArray[i][1]);
            }
     
        }

    I'm doing something wrong but I'm not sure what since nothing shows up in the text area. Assuming that I am storing the data in the array properly, am I referencing it wrong somehow when I try to use the runReport button? Thanks again for the help.
    Last edited by drixnak; October 19th, 2010 at 12:58 PM.

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Populating a 2D array with textfield values

    That loop will never be entered, as 0 is not < 0.

  10. #10
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Populating a 2D array with textfield values

    LOL Yeah, good point. Got to fix that.

     
    private void runReportActionPerformed(java.awt.event.ActionEvent evt) {                                          
     
           for (int i = 0; i < ptArray.length; i++){
                reportArea.append("Time: " + ptArray[i][0] + "Payment: " + ptArray[i][1]);
            }

    Ok, I set it to this and it works good. Problem now is that it's brought another issue since it appears to run through the full 20 rows that I set in the initial declaration at

     
    double[][] ptArray = new double[20][2];

    This is something I had trouble understanding earlier. I really wanted to set this to i or something so that there isn't a set variable and instead it creates a new row each time I enter information. I kept getting errors though with everything I tried. Also, it appears to only keep 1 row in memory instead of adding a new row for each pair of data entered. Any ideas? Thanks again for all the posts. It's been incredibly helpful.
    Last edited by drixnak; October 19th, 2010 at 01:29 PM.

Similar Threads

  1. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM
  2. Replies: 1
    Last Post: March 22nd, 2010, 04:34 PM
  3. Populating a JTable
    By crism85 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 20th, 2010, 01:57 PM
  4. Assgining values to array indexes
    By chronoz13 in forum Collections and Generics
    Replies: 3
    Last Post: December 28th, 2009, 11:09 PM
  5. Substitution of Values in Array
    By nyeung in forum Collections and Generics
    Replies: 2
    Last Post: October 26th, 2009, 08:02 PM