Results 1 to 8 of 8
- 06-18-2011, 06:03 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 5
- Rep Power
- 0
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
- 06-18-2011, 06:08 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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.
- 06-18-2011, 06:15 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 5
- Rep Power
- 0
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.
- 06-18-2011, 06:24 PM #4
they always replace the first two entriesThat's exactly what the two lines of code do: replace the first two entries.Java Code:rh1[counter][0]= num8; rh1[counter][1]= num9;
Instead of hardcoding indexes of 0 and 1, use variables to put the numbers somewhere else in the array.
- 06-18-2011, 06:51 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 5
- Rep Power
- 0
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 :)
- 06-18-2011, 09:03 PM #6
Member
- Join Date
- Jun 2011
- Posts
- 5
- Rep Power
- 0
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
- 06-18-2011, 09:08 PM #7
You're the one that has to answer that. Does the code do what you want it to do?is that correct?
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
- 06-18-2011, 09:14 PM #8
Member
- Join Date
- Jun 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Problem in inserting values into Oracle DB
By techsing14 in forum Java ServletReplies: 4Last Post: 04-13-2011, 01:52 AM -
inserting values in to array
By Dayanand in forum New To JavaReplies: 8Last Post: 03-05-2011, 04:23 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
inserting values from jtable into database
By sandeepsai17 in forum New To JavaReplies: 1Last Post: 06-29-2009, 07:31 PM -
Problem inserting values to MySQL tables from the Data Source Explorer
By tip in forum EclipseReplies: 0Last Post: 12-24-2007, 09:47 AM


LinkBack URL
About LinkBacks
Reply With Quote.gif)

Bookmarks