# Consuming even and odd numbers from an array

Page 1 of 1

## 6 Replies - 5760 Views - Last Post: 17 April 2012 - 07:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=275536&amp;s=b9d53c08b4ffc94ed95d0f99fcbddea9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ola3

Reputation: 0
• Posts: 21
• Joined: 17-February 12

# Consuming even and odd numbers from an array

Posted 16 April 2012 - 08:15 PM

I'm having difficulty getting the odd numbers to go into the odd array and the even numbers to go into the even array

```public class findeven{

public static void main(String[] args){
int [] number= new int[]{1,2,3,4,5,6,7,8,9};
int[] even;
int[] odd;
even = new int[10];
odd = new int[10];
for( int i=0; i<number.length;i++){
if(number[i]%2==0){
number[i]=even;
}
else
number[i]=odd;
}
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Consuming even and odd numbers from an array

### #2 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: Consuming even and odd numbers from an array

Posted 16 April 2012 - 08:18 PM

You're setting an int to int[]. That's wrong. If you're not going to put any value in the arrays int[] even and int[] odd, what's the point of declaring them?

### #3 ola3

Reputation: 0
• Posts: 21
• Joined: 17-February 12

## Re: Consuming even and odd numbers from an array

Posted 16 April 2012 - 08:25 PM

so i have changed it but not im getting a new error
```import java.util.*;
public class findeven{

public static void main(String[] args){
int [] number= new int[]{1,2,3,4,5,6,7,8,9};

int[] odd;
even = new int[10];
odd = new int[10];
for( int i=0; i<number.length;i++){
if(number[i]%2==0){
number[i]=int[] even;
}
else
number[i]=int [] odd;
}
}
}
```

File: C:\Users\Chimaroke\Desktop\Rahman\Computer science\spring 2012\Homework\findeven.java [line: 12]
Error: C:\Users\Chimaroke\Desktop\Rahman\Computer science\spring 2012\Homework\findeven.java:12: '.class' expected
File: C:\Users\Chimaroke\Desktop\Rahman\Computer science\spring 2012\Homework\findeven.java [line: 15]
Error: C:\Users\Chimaroke\Desktop\Rahman\Computer science\spring 2012\Homework\findeven.java:15: '.class' expected

### #4 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: Consuming even and odd numbers from an array

Posted 16 April 2012 - 08:28 PM

What are you trying with this? You've absolutely lost me,

### #5 ola3

Reputation: 0
• Posts: 21
• Joined: 17-February 12

## Re: Consuming even and odd numbers from an array

Posted 16 April 2012 - 08:31 PM

the array numbers has numbers from 1-9

what i want to do is to take the odd numbers in the array called numbers and put them into a new array called odd
and i want to do thesame for even numbers but put the even numbers in an array called even

### #6 collegekid901

Reputation: 8
• Posts: 41
• Joined: 09-March 12

## Re: Consuming even and odd numbers from an array

Posted 16 April 2012 - 09:33 PM

The problem is you are not using arrays properly. when you trying to insert a value from the number array it should look something like

even/odd[index value]= numbers[i]

you need to figure out a way to determine the index value of the odd or even arrays.

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8369
• Posts: 31,956
• Joined: 06-March 08

## Re: Consuming even and odd numbers from an array

Posted 17 April 2012 - 07:00 PM

you need two more variables: nbEven and nbOdd and update them when you detect one or the other

```int [] number= new int[]{1,2,3,4,5,6,7,8,9};
int[] odd = new int[number.length]; // worst case
int[] even = new int[number.length];
int nbOdd = 0, nbEven = 0;

for(int i = 0; i < number.length; ++i) {
if(number[i] % 2 == 0)
even[nbEven++] = number[i];
else
odd[nbOdd++] = number[i];
}

```

now you have nbOdd numbers in odd[] and nbEven numbers in even[]

Happy coding