6 Replies - 3781 Views - Last Post: 17 April 2012 - 07:00 PM Rate Topic: -----

#1 ola3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 ola3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • 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,
Was This Post Helpful? 0
  • +
  • -

#5 ola3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 collegekid901  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1