• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help: traverse through one array and store in another array.

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Kalona,"

Welcome to JavaRanch! Please check your private messages by clicking on My Private Messages. Thanks!
 
Kalona Ark
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, marc weber, for helping me with this. And you too, bart zagers, for your advice. I finally got it to work.
 
reply
    Bookmark Topic Watch Topic
  • New Topic