Results 1 to 8 of 8
  1. #1
    rholmes464 is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default Inserting new values into Array after inital popluation

    I have been reading these forums and have not found the answer so please excuse if my first post is a little ...not organized?
    I have an Multidimension Array that I am trying to populate via user input (have to use array) and that I can do, for the first 2 numbers. My issue is subsequent numbers (in the same session) - they always replace the first two entries. My thought process was that I would check to see if there was already data in the array and then insert the new data after the current info. But Alas, I have not been able to figure it out. Not asking to solve, but pointing me in the right direction would be greatly appreciated. (will try to post code below)
    <code>
    public void rh_add_arrays(double num8,double num9){
    for(counter=0; counter<1;++counter)
    {
    for(counter2=0;counter2<=counter;counter2++)
    {
    if(counter > rh1.length)
    {
    ** have not put the error message here yet**
    }
    else{
    rh1[counter][0]= num8;
    rh1[counter][1]= num9;
    System.out.println("made it here");
    }
    }
    }
    </code>
    I tried to check the rh1.index value and havent been able to make it fail (i.e. if rh1.length is not filled do this)
    Thank you for your time

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Consider using a List (for example an ArrayList (Java Platform SE 6) ), which will allow you to readily append values to the object...further, rather than having a 2 index arrays, create a class which holds these values should the values be associated with each other.

  3. #3
    rholmes464 is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the quick response, I actually started out with an ArrayList (which was very easy to do) but the instructor is requiring an array. It's been about 7 years since doing java and well I am extremely rusty.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    they always replace the first two entries
    Java Code:
    rh1[counter][0]= num8;
    rh1[counter][1]= num9;
    That's exactly what the two lines of code do: replace the first two entries.
    Instead of hardcoding indexes of 0 and 1, use variables to put the numbers somewhere else in the array.

  5. #5
    rholmes464 is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    I changed from
    rh1[counter][0] = num8;
    rh1[counter][1] = num9;

    to
    <code>
    rh1[counter][counter2]= num8;
    rh1[counter2][counter2]= num9;
    </code>
    and so far through testing it is working

    thanks to all for the advice :)

  6. #6
    rholmes464 is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    Upon changing the code to this
    <code>
    for(counter=0; counter<2;++counter)
    {

    for(counter2=0;counter2<rh1[counter].length;++counter2)
    {
    rh1[counter][a] = num8;
    rh1[counter2][j]= num9;
    }
    ++counter;
    a++;
    j++;
    }
    It increments but i dont know if it is doing it correctly - while debugging the code I notice that it is populating the array thusly
    1st run : input : 1, 2 -> rh1(0) : [0][1] : 1.0 / 0.0 rh1(1): [1][0]: 2.0 /0.0
    2nd run: 11,22 -> rh1(0): [0][1]: 1.0/11.0 rh1(1): [1][0]: 2.0 /22.0
    so excuse the stupid question - but is that correct?
    Last edited by rholmes464; 06-18-2011 at 09:04 PM. Reason: made mistake

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    is that correct?
    You're the one that has to answer that. Does the code do what you want it to do?

    You are incrementing the variable: counter two times. Is that what you want?

    Normally the incrementing is by post operator: count++


    Please put your code in code tags when posting. See: BB Code List - Java Forums
    Use the # icon above the input area

  8. #8
    rholmes464 is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    I apologize if I misspoke - It is incrementing like I want, my question arose from me thinking that it would increment [0][1] - 1st set then [1][1] next set, and so on and so forth - it's most likely my not understanding of how the arrays increment - more research to do
    thanks!

Similar Threads

  1. Problem in inserting values into Oracle DB
    By techsing14 in forum Java Servlet
    Replies: 4
    Last Post: 04-13-2011, 01:52 AM
  2. inserting values in to array
    By Dayanand in forum New To Java
    Replies: 8
    Last Post: 03-05-2011, 04:23 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. inserting values from jtable into database
    By sandeepsai17 in forum New To Java
    Replies: 1
    Last Post: 06-29-2009, 07:31 PM
  5. Replies: 0
    Last Post: 12-24-2007, 09:47 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
  •