Results 1 to 10 of 10
  1. #1
    almostthere is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Unhappy Looking for help printing out a 2d array. Format is wrong?

    I am new to Java, so please forgive my ignorance! I am building a gui in NetBeans IDE and can't get a 2d array to print out correctly. I have been messing with this for a few days with no luck!

    I am passing data into a 2d array with text fields and an enter button.

    Once the data is entered (assignment includes three sets of data) the user will hit the report button which will print out the array in a nice format as well as some other calculations.

    The main problem is the array is being outputted like this:

    Minutes = 60.0 Earnings = $10.0Minutes = 60.0 Earnings = $10.0
    Minutes = 120.0 Earnings = $40.0Minutes = 120.0 Earnings = $40.0
    Minutes = 90.0 Earnings = $15.0Minutes = 90.0 Earnings = $15.0
    Minutes = 0.0 Earnings = $0.0Minutes = 0.0 Earnings = $0.0
    Minutes = 0.0 Earnings = $0.0Minutes = 0.0 Earnings = $0.0
    Minutes = 0.0 Earnings = $0.0Minutes = 0.0 Earnings = $0.0
    Minutes = 0.0 Earnings = $0.0Minutes = 0.0 Earnings = $0.0
    Minutes = 0.0 Earnings = $0.0Minutes = 0.0 Earnings = $0.0
    Minutes = 0.0 Earnings = $0.0Minutes = 0.0 Earnings = $0.0
    Minutes = 0.0 Earnings = $0.0Minutes = 0.0 Earnings = $0.0

    When it should look like this:

    Minutes = 60.0 Earnings = $10.0
    Minutes = 120.0 Earnings = $40.0
    Minutes = 90.0 Earnings = $15.0

    Why do the entries repeat, and how do I stop the empty array values from displaying?

    I can start by posting the areas of interest, but if any more is needed to help please let me know!

    Java Code:
      
    
    //Declaring array first
    
    //initial declaration of variables for user entered data
        double minIn = 0, payIn = 0;
        //declare 2d array
        int rowIndex = 0;
        static final int maxROWS = 10;
        static final int maxCOLS = 2;
        double[][] inputData = new double[maxROWS][maxCOLS];
        //declare variables
        double totalMin = 0;
        double totalPay = 0;
        double minWage = 6.55;
        double avgWage = 0;
        final int hour = 60;
        int printHeader = 0;
    
    
    
    //This below is code for the Enter Button
    
    try {
                minIn = Double.parseDouble(inputMinutes.getText());
                payIn = Double.parseDouble(inputPayment.getText());
    
                if (minIn > 0 && minIn < 241); else {
                    outputTextArea.append("Invalid input, please enter a valid number of minutes.\n");
                    return;
                }
                if (payIn >= 0); else {
                    outputTextArea.append("Invalid input, please enter a valid payment amount.\n");
                    return;
                }
            } catch (Exception A) {
                outputTextArea.append("Invalid input, please input a numerical value.\n");
                return;
    
            }
    
            //store input into array
            if (rowIndex < 10) {
                inputData[rowIndex][0] = minIn;
                inputData[rowIndex][1] = payIn;
    
                //increment counter by one
                rowIndex++;
            }
    
    
    
    //The below is code for the output being triggered by the Report Button
    
     //clear Text Area for new printout
            outputTextArea.setText("");
    
            //nested for loop to print 2d array contents
            for (int i = 0; i < maxROWS; i++) {
                for (int g = 0; g < maxCOLS; g++) {
                    outputTextArea.append("Minutes = " + inputData[i][0] + " " + "\t" + "Earnings = $" + inputData[i][1]);
                }
                outputTextArea.append("\n");
            }

    Any assistance/corrections would be greatly appreciated!

    Thank you ahead of time!!!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Looking for help printing out a 2d array. Format is wrong?

    maxCOLS is 2, so the inner fow loop goes round twice, appending twice.
    Please do not ask for code as refusal often offends.

  3. #3
    almostthere is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: Looking for help printing out a 2d array. Format is wrong?

    Thanks for the tip Tolls!

    That makes sense. I replaced maxCOLS with "1"

    Output is looking better after some tweaking!

    Minutes = 60.0 Earnings = $10.0
    Minutes = 120.0 Earnings = $40.0
    Minutes = 90.0 Earnings = $15.0
    Minutes = 0.0 Earnings = $0.0
    Minutes = 0.0 Earnings = $0.0
    Minutes = 0.0 Earnings = $0.0
    Minutes = 0.0 Earnings = $0.0
    Minutes = 0.0 Earnings = $0.0
    Minutes = 0.0 Earnings = $0.0
    Minutes = 0.0 Earnings = $0.0

    How about getting rid of those empty values?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: Looking for help printing out a 2d array. Format is wrong?

    Quote Originally Posted by almostthere View Post
    That makes sense. I replaced maxCOLS with "1"
    What's the point? If you're only doing it once, you don't need a loop.

    To get rid of the empty values, try replacing maxROWS with rowIndex in your loop.

    Regards,
    Jim
    Last edited by jim829; 09-20-2013 at 04:25 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Looking for help printing out a 2d array. Format is wrong?

    correction: you don't need a nested for-loop, just the one for the rows. And you don't need a 2D array either.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    almostthere is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: Looking for help printing out a 2d array. Format is wrong?

    Quote Originally Posted by jim829 View Post
    What's the point? If you're only doing it once, you don't need a loop.

    To get rid of the empty values, try replacing maxROWS with rowIndex in your loop.
    Thanks Jim! That solved that problem.

    As far as the nested for loop, it is a requirement for the assignment. Perhaps I can reformat the loop to take advantage of a nested for loop.

    Quote Originally Posted by gimbal2 View Post
    correction: you don't need a nested for-loop, just the one for the rows. And you don't need a 2D array either.
    These are both requirements for the assignment. Hmm...


    As of right now it is printing correctly, but I am sure the code is not "correct".

    Thank you all for the feedback so far.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Looking for help printing out a 2d array. Format is wrong?

    Then I suspect the format ought to be:
    Minutes Earnings
    <mval> <eval>
    <mval> <eval>
    <mval> <eval>

    Otherwise the nested loops makes no sense.
    Please do not ask for code as refusal often offends.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: Looking for help printing out a 2d array. Format is wrong?

    Quote Originally Posted by almostthere View Post
    Perhaps I can reformat the loop to take advantage of a nested for loop.
    I would definitely recommend against that. You may want to re-evaluate the assignment or even talk to the prof. Forcing someone to write unnecessary code is not conducive to good programming practices. There are many examples that could be used for teaching which would lend themselves to using nested loops and 2d arrays.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    almostthere is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: Looking for help printing out a 2d array. Format is wrong?

    Quote Originally Posted by Tolls View Post
    Then I suspect the format ought to be:
    Minutes Earnings
    <mval> <eval>
    <mval> <eval>
    <mval> <eval>

    Otherwise the nested loops makes no sense.
    I suppose that would make more sense. The assignment specifies that the format be in the way I posted above, so perhaps the instructions do not match the assignment very well.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Looking for help printing out a 2d array. Format is wrong?

    In which case the assignment makes no sense at all, as you've discovered.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2013, 02:36 PM
  2. How can I format this multi dimentional array?
    By EscSequenceAlpha in forum New To Java
    Replies: 3
    Last Post: 05-14-2012, 03:50 AM
  3. problem in a method which return array format
    By elenora in forum Advanced Java
    Replies: 13
    Last Post: 09-21-2011, 09:43 AM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  5. How Convert a Byte array to a image format
    By perlWhite in forum New To Java
    Replies: 2
    Last Post: 08-23-2009, 08:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •