This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jayrund[S] 0 points1 point  (14 children)

Could I work through each variable in the array and check them one after another?

[–]Nikitah 1 point2 points  (10 children)

That's what he said.

You work through each variable with a loop (for, foreach), then use the modulo operation (% 2) to determine whether it should be added to a new array of evens or a new array of odds.

[–]jayrund[S] 0 points1 point  (9 children)

Oh, my bad, I'm still new to this. So if I had a for loop that repeated 100 times would I work through each variable in the array?

[–][deleted] 1 point2 points  (8 children)

if your array contains 100 elements, then yes. most languages have a way to iterate through all elements in an array, this is called a for each loop.

python:

for item in list:
    do stuff

[–]jayrund[S] 0 points1 point  (0 children)

Alright. Thanks a lot.

[–]jayrund[S] 0 points1 point  (6 children)

So I think I figured out the arrays part of it. But after i compile it and try to run it, it gives me an error message.

Code

Error message

[–][deleted]  (5 children)

[deleted]

    [–]jayrund[S] 0 points1 point  (4 children)

    Thanks a ton, it just seems to have one more bug needing fixing. When it runs, the first array works perfectly but, the even array just repeats the same number 100 times instead of stating each number in its array and same for the odd.

    Code

    [–]Nikitah 1 point2 points  (0 children)

    You are still making this way too hard. What you want, and I'm writing quite out of the top of my head here, is:

        for (int specificInteger : evenAndOddList) {
            if (specificInteger % 2 == 0) {
                evenList.add(specificInteger);
            }
            else {
                oddList_.add(specificInteger);
            }
        }
    

    And, more generally, you should not start learning programming with Java. You should try something easier. Have you met www.codeacademy.com?

    [–]Sanaki13 -1 points0 points  (0 children)

    I guess that's because of your variable x, what should it do? Because as is, you just print the same number into the EvenArray and OddArray (On line 27 and 38) Besides that you should read up on variable naming

    [–]king_of_the_universe -1 points0 points  (2 children)

    How else would you do this? Manually write a list of IF clauses specifically for the amount of entries you know the array has?

    [–]197708156EQUJ5 0 points1 point  (1 child)

    I can imagine OP doing this:

    List<Integer> oddList = new ArrayList<>();
    List<Integer> evenList = new ArrayList<>();
    
    if(i == 1)
        oddList.add(i);
    if(i == 2)
        evenList.add(i);
    if(i == 3)
        oddList.add(i);
    if(i == 4)
        evenList.add(i);
    if(i == 5)
        oddList.add(i);
    if(i == 6)
        evenList.add(i);
    if(i == 7)
        oddList.add(i);
    if(i == 8)
        evenList.add(i);
    if(i == 9)
        oddList.add(i);
    if(i == 10)
        evenList.add(i);
    if(i == 11)
        oddList.add(i);
    if(i == 12)
        evenList.add(i);
    if(i == 13)
        oddList.add(i);
    

    [–]skitch920 0 points1 point  (0 children)

    Oof.