odd & even array

Hi, can someone please tell me why this is not working? I am supposed to have a user input numbers into an array. If the numbers are even they go into the even ArrayList and for odd, vice versa.

publicstaticvoid main(String[] args)

{

int num,xy=0, MAX = 10;

int [] numbers =newint [MAX];

EasyReader console =new EasyReader();

ArrayList evenNumbers =new ArrayList();

ArrayList oddNumbers =new ArrayList();

for (num=0; num<=MAX; num++)

{

System.out.println("Please enter a number to put into the array: ");

numbers[num]=console.readInt();

}

for (int x=0; x<=MAX; x++)

{

Integer some=new Integer(numbers[x]);

if((numbers[num]%2)==0)

{

evenNumbers.add(new Integer(x));

}

else

{

oddNumbers.add(new Integer(x));

}

}

for (int i=0; i<numbers.length; i++)

{

System.out.println("The array is:" +numbers[i]);

}

for (int i=0; i><oddNumbers.size(); i++)

{

System.out.println("The odd arrayList is: "+oddNumbers.get(i));

}

for (int i=0; i><evenNumbers.size(); i++)

{

System.out.println("The even arrayList is: "+evenNumbers.get(i));

}

}

Thanks.>

[2608 byte] By [Dramakweena] at [2007-11-26 18:38:01]
# 1
By the way, the program also has to print out the array and ArrayLists at the end.
Dramakweena at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 2
Why not get rid of the numbers array, move the if statement into your first loop and get rid of the second loop.loop {get user inputif input is even {insert into even list} else{insert into odd list}}
floundera at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 3
> Hi, can someone please tell me why this is not working? Can you be more specific? In what way is it not working?
DrLaszloJamfa at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 4
I get an exception 11 and something seems off
Dramakweena at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 5
thats a good idea, but the numbers array is a part of the assignment
Dramakweena at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 6
BTW here is the problem:evenNumbers.add(new Integer(x));Hint: why create the Integer variable some if you never use it?
floundera at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 7
i thought i might need it, but can you explain to me whats wrong with the evenNumbers.add(new Integer(x)); ?
Dramakweena at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 8
for (int x=0; x<=MAX; x++)Arrays are index 0 to length - 1.
floundera at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 9

By "exception 11" I think you mean an ArrayIndexOutOfBoundsException.

Take a look at these lines:

int num, MAX = 10;

int [] numbers = new int [MAX];

for (num=0; num<=MAX; num++) {

numbers[num]=...

}

If array numbers has 10 elements they are stored in numbers[0] to numbers[9] -- there is no numbers[MAX]. So loops that iterate over arrays should be written:

for (int num=0; num<numbers.length; num++) {

You did that further down in the code, too.

>something seems off

?

DrLaszloJamfa at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 10
Oh, I see what you're saying but I don't know how to fix that in the 2nd for loop or elsewhere
Dramakweena at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 11
evenNumbers.add(new Integer(x));What is wrong is that you are adding x to the ArrayList not the number in your array.
floundera at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 12

that makes sense but what is the error here: for (int x=0; x<=MAX; x++)

{

if((numbers[num]%2)==0)

{

evenNumbers.add(new Integer(num));

}

else

{

oddNumbers.add(new Integer(num));

}

}

Dramakweena at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 13

> that makes sense but what is the error here:

> for (int x=0; x<=MAX; x++)

>{

>if((numbers[num]%2)==0)

>{

> evenNumbers.add(new Integer(num));

>}

> else

>{

> oddNumbers.add(new Integer(num));

>}

> }

1. You are using two indexes: x and num.

2. You add the indexadd(new Integer(num))to a list instead of the data value at that index in the array.

DrLaszloJamfa at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 14
As I said earlier, arrays are indexed from 0 to length - 1. So for your array of length 10 the indicies are 0 to 9. What happens when x = 10?
floundera at 2007-7-9 6:12:05 > top of Java-index,Java Essentials,New To Java...
# 15
i'm confused by the 2nd thing you're saying. how do you fix that?
Dramakweena at 2007-7-21 17:26:23 > top of Java-index,Java Essentials,New To Java...
# 16

> i'm confused by the 2nd thing you're saying. how do you fix that?

Look at this snippet:

if((numbers[num]%2)==0)

{

evenNumbers.add(new Integer(num));

}

Suppose num is 1. If numbers[1] is an even number, you store 1 in the list. Don't store 1, store the even number. How can you do that?

DrLaszloJamfa at 2007-7-21 17:26:23 > top of Java-index,Java Essentials,New To Java...
# 17
that makes sense but i keep getting an exception for if((numbers[num]%2)==0) that portion of my code supposedly. I changed the num to numbers[num] that gets added too
Dramakweena at 2007-7-21 17:26:23 > top of Java-index,Java Essentials,New To Java...
# 18
What exception?
DrLaszloJamfa at 2007-7-21 17:26:23 > top of Java-index,Java Essentials,New To Java...
# 19
ArrayIndexOutOfBounds exception
Dramakweena at 2007-7-21 17:26:23 > top of Java-index,Java Essentials,New To Java...
# 20
> ArrayIndexOutOfBounds exceptionSee replies 8, 9 and 14
DrLaszloJamfa at 2007-7-21 17:26:23 > top of Java-index,Java Essentials,New To Java...
# 21
It should be x, your loop variable!
floundera at 2007-7-21 17:26:24 > top of Java-index,Java Essentials,New To Java...
# 22
It might be time for you to take a break. Go and have a smoke/beer/food whatever your poison and clear your head. When you come back grab a piece of paper and write down all your variables and their values and trace through your program so you can see what is happening.
floundera at 2007-7-21 17:26:24 > top of Java-index,Java Essentials,New To Java...
# 23
In that case, I'll have a beedi/Kingfisher/chicken tikka
DrLaszloJamfa at 2007-7-21 17:26:24 > top of Java-index,Java Essentials,New To Java...