| Author |
Help: traverse through one array and store in another array.
|
Kalona Ark
Greenhorn
Joined: Oct 31, 2007
Posts: 20
|
|
int[] allNumbers = new int[20]; int[] oddNumbers = new int[10]; int j = 0; for (int i = 0; i < allNumbers.length; i++){ if (i % 2 == 1) allNumbers[i] = i; oddNumbers[j] = allNumbers[i]; System.out.println(oddNumbers[j]); } output: 1 1 3 3 5 5 7 7 9 9 11 11 13 13 15 15 17 17 19 19 19 I am having a problem with storing the odd numbers to the oddNumbers array.
|
 |
bart zagers
Ranch Hand
Joined: Feb 05, 2003
Posts: 234
|
|
I think you have more problems than just the oddNumbers array. Do you realize you are only setting half of the allNumbers array (you have no curly braces after the if statement)? And you never increment the j variable. Maybe you should do the printing in a separate for loop, it might help showing you what is going on. [ November 02, 2007: Message edited by: bart zagers ]
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
"Kalona," Welcome to JavaRanch! Please check your private messages by clicking on My Private Messages. Thanks!
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Kalona Ark
Greenhorn
Joined: Oct 31, 2007
Posts: 20
|
|
And you never increment the j variable.
I know that I increment the j variable, I would use this symbol: j++. My problem is I do not know where to put it. I know with this program I have been reading into it too much. So yesterday I took a nap and re-did the whole thing again. I knew I had to increment it, I just did not know where to put it to get it to work. I used the System.out.println() to see what was going on; it is not really suppose to be there.
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Kalona Ark: ...I know that I increment the j variable, I would use this symbol: j++. My problem is I do not know where to put it...
j++; is a statement by itself, so you can add it as its own line. I would suggest carefully describing the process in English before coding it in Java. This will help you refine the logic and identify the steps.
|
 |
Kalona Ark
Greenhorn
Joined: Oct 31, 2007
Posts: 20
|
|
When I do compile and run it, nothing appears. // Second, traverse through the allNumbers array and determine all the the odd numbers (use the modulus operator %) and // store all the odd numbers in the oddNumbers array. Again, use looping logic and do not hard code the values. int j = 0; for (int i = 0; i < allNumbers.length; i++){ if (i % 2 == 1){ allNumbers[i] = i; oddNumbers[j] = allNumbers[i]; j++; } } // Third, print the the contents of the oddNumbers array using looping logic. for (j = 0; j > oddNumbers.length; j++){ System.out.println(oddNumbers[j]); }
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Kalona Ark: ...for (j = 0; j > oddNumbers.length; j++){...
Check the boolean condition in your loop for printing the contents of oddNumbers. Once you fix this simple error, I think you will like the output. However, you might not like the output of printing the contents of the other array, allNumbers. Note that your code assigns explicit values to both arrays (oddNumbers and allNumbers) only if i is odd (i%2 == 1). [ November 02, 2007: Message edited by: marc weber ]
|
 |
Kalona Ark
Greenhorn
Joined: Oct 31, 2007
Posts: 20
|
|
Thank you, marc weber, for helping me with this. And you too, bart zagers, for your advice. I finally got it to work.
|
 |
 |
|
|
subject: Help: traverse through one array and store in another array.
|
|
|