Results 1 to 7 of 7
  1. #1
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Second number not getting into 2D array

    I'm kinda puzzled here as to why the second number the user inputs is not getting into the array.

    I know my code is poor, but I'm just getting to know java and it doesn't like me.

    Java Code:
       private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // Define variables
            double minutesIn = 0; double paymentIn = 0;
            
            //Get input (minutes and payment) from user
            
            try{
            minutesIn = Double.parseDouble(minutesField.getText());
            paymentIn = Double.parseDouble(paymentField.getText());
            if(minutesIn>=0);
            else{
                    outputTextArea.append("Please enter a vaild number of minutes\n");
            return;
            }
            if(paymentIn>=0);
            else{
                    outputTextArea.append("Please enter a vaild payment amount\n");
            return;
            }
            
            }
            catch(Exception e)
            {
              outputTextArea.append("Please input a numerical value\n");
                return;  
            }
            
            // Store input into array
            
            double[][] userInput = {{minutesIn},{paymentIn}};
            System.out.println(userInput[0][0] + "\t");
            System.out.println(userInput[0][1]);
    The last 2 System.out.println lines are just for me to see what the values are in the console, for debugging purposes.
    The first value is fine, I can see whatever I enter. The second value produces an ArrayIndexOutOfBoundsException.
    Not sure why as I have declared the array to be 2D.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Second number not getting into 2D array

    The error on your code is that you are accessing the array outside it range. You have an array named userInput which length is 2. The first element is another array with one element. The second element is also an array with one element. If you need to take out the minuteIn you should access it using userInput[0][0]. And to access the paymentIn your indexes are userInput[1][0] not userInput[0][1].

  3. #3
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Second number not getting into 2D array

    Quote Originally Posted by wsaryada View Post
    The error on your code is that you are accessing the array outside it range. You have an array named userInput which length is 2. The first element is another array with one element. The second element is also an array with one element. If you need to take out the minuteIn you should access it using userInput[0][0]. And to access the paymentIn your indexes are userInput[1][0] not userInput[0][1].
    I just tried that and you're right! Thanks.

    Follow up question - I thought that 2D arrays were stored [row][column]
    Maybe I've declared the array incorrectly. I'm trying to get 2 columns to add up later on. The first being minutesIn and the second being paymentIn.
    It looks like all the input is going to the first column or am I way off here somehow.
    Last edited by neveser; 11-30-2012 at 02:00 AM.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Second number not getting into 2D array

    Well, if you considering a 2D array as in row and column format try to picture it this way:

    Java Code:
    double[][] userInput = {
                    {minutesIn}, // <- your first row, contains a single column
                    {paymentIn}  // <- your second row, contains a single column
            };

  5. #5
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Second number not getting into 2D array

    So my declaration statement is wrong for what I'm trying to do.

    This is what I have pictured in my head

    | column 1 | column 2 |
    |-----------|-----------|
    | minutesIn | paymentIn |

    How would I do that?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Second number not getting into 2D array

    A one dimensional array of double will do, unless you envision multiple rows. If so, then you'd want:

    { {row1Item1, row1item2}, {row2Item1, row2Item2}}

  7. #7
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Second number not getting into 2D array

    Thank you Fubarable!
    Just had a couple of extra curly brackets. :)
    Oh, I was after multiple rows.

Similar Threads

  1. Need help with inserting a new number into an array
    By Get_tanked in forum New To Java
    Replies: 7
    Last Post: 01-21-2011, 02:36 AM
  2. Find the second largest number in the array
    By radhi16 in forum New To Java
    Replies: 5
    Last Post: 01-13-2011, 05:08 PM
  3. Find max number in 2D array?
    By spatel14 in forum New To Java
    Replies: 3
    Last Post: 06-30-2010, 04:27 PM
  4. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 12:37 AM
  5. Number Array to Sound
    By Phantasmagorical in forum Advanced Java
    Replies: 8
    Last Post: 03-01-2009, 05:36 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
  •