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 5 of 5

Thread: Store Values in 2D array and output to textarea

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Store Values in 2D array and output to textarea

    I have a java GUI that contains 2 text boxes and 2 buttons. When the enter button is pushed it needs to store the values in the text boxes in a 2D array and clear the text boxes. When the report button is pushed it needs to output all values in the 2D array to a text area. Below is the code that I have so far. It appears that it is not adding all the values to the rows as it loops and I would like to know how to ouput all values in the array to a textarea. Any help would be greatly appreciated. Thank You


    public class TutoringSessionUI extends javax.swing.JFrame {
        double [][] tutorArray = new double[1][2];
     
        private void EnterButtonActionPerformed(java.awt.event.ActionEvent evt) {
     
            for(int j = 0; j < tutorArray.length; j++){
                for(int k = 0; k < tutorArray.length; k++){
     
                    //double minutesInt = 0;
                    //double moneyInt = 0;
     
                    double minutesInt = Double.parseDouble(TimeText.getText());
                    double moneyInt= Double.parseDouble(MoneyText.getText());
     
                     if (minutesInt < 0)
                {
                    IllegalArgumentException exception
                    = new IllegalArgumentException("Invalid payment amount.");
                    throw exception;
            }
     
            //Checks for negative numbers in Money and throws exception if found.
            if (moneyInt < 0)
            {
                IllegalArgumentException exception
                = new IllegalArgumentException("Invalid time amount.");
                throw exception;
            }
     
                    tutorArray[j][0] = minutesInt;
                    tutorArray[j][1] = moneyInt;
     
                    System.out.println(tutorArray[j][0]);
                    System.out.println(tutorArray[j][1]);
                    System.out.println();
     
     
                    TimeText.setText("");
                    MoneyText.setText("");
     
                }
            }
        }
     
        private void QuitButtonActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
            System.exit(0);
        }
     
        private void ReportButtonActionPerformed(java.awt.event.ActionEvent evt) {
    double tutorOutput = tutorArray[0][0] + tutorArray[0][1];
    String tutorOutPut = Double.toString(tutorOutput);
    ReportArea.append(tutorOutPut);
     
        }
    Last edited by raverz1tildawn; January 7th, 2011 at 04:35 PM. Reason: Modify code to only include valuable information


  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: Store Values in 2D array and output to textarea

    When posting code, it should be in the form of an SSCCE. People aren't going to want to read through all that yucky GUI-editor code.

    You should put some print statements inside your nested for loops (or better yet, use a debugger) to see what values j and k are reaching. Compare that to what values should they be reaching.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Store Values in 2D array and output to textarea

    Thanks for the quick response Kevin and I have edited the code to only include the relevant parts. Sorry for the oversight. I have placed print lines in the loops and it shows values for j and k always as 0 which is my issue. It never holds the value of j or k anywhere for the next event to add to that number.

  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: Store Values in 2D array and output to textarea

    Quote Originally Posted by raverz1tildawn View Post
    Thanks for the quick response Kevin and I have edited the code to only include the relevant parts. Sorry for the oversight. I have placed print lines in the loops and it shows values for j and k always as 0 which is my issue. It never holds the value of j or k anywhere for the next event to add to that number.
    Thanks, although technically an SSCCE should be executable.

    Your problem is in the values to which you're limiting your inner for loop. What is tutorArray.length? What do you want the loop's upper limit to be?

    Since a 2D array is just an array of arrays, you can do stuff like this:
    tutorArray[j] = new int[]{2, 3, 4};

    It also means you can do something like this:
    tutorArray[j].length
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Store Values in 2D array and output to textarea

    The end result is that i have a 2D array that 2 columns and unlimited rows. Every time the enter button is pushed it stores the values in the 2 text fields in the array. When the report button is pushed it outputs all the values in the array to a textarea.

Similar Threads

  1. TextArea and Array Problem
    By slippery_one in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 28th, 2010, 07:11 AM
  2. Printing boolean values from an array
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 12:11 AM
  3. Replies: 1
    Last Post: April 7th, 2010, 03:44 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