0
public class AssignmentChapter8
{
    public static void main(String[] args)
    {
        int randomNumbers[] = new int[100];
        int oddCount;
        int evenCount;

        for(int x = 0; x < randomNumbers.length; x++)
            randomNumbers[x] = (int)(Math.random() * 25);

        for(int y = 0; y < randomNumbers.length; y++)
            if(randomNumbers[y] % 2 > 0)
                oddCount += 1;
            else
                evenCount+=1;


        int oddNumbers[] = getOddNumbers(oddCount, randomNumbers[]);
        int evenNumbers[] = getEvenNumbers(evenCount, randomNumbers[]);

        System.out.println();
        System.out.println("The list of odd numbers is:");
        System.out.println();

        for(int a = 0; a < oddNumbers.length; a++)
            System.out.print(oddNumbers[a] + "\t");

        System.out.println();
        System.out.println("The list of even numbers is:");
        System.out.println();

        for(int b = 0; b < evenNumbers.length; b++)
            System.out.print(evenNumbers[b] + "\t");
    }
    public static int[] getOddNumbers(int oddCount, int randomNumbers[])
    {
        int oddNumbers[] = new int[oddCount];
        int counter = 0;

        for(int x = 0; x < randomNumbers.length; x++)
            if(randomNumbers[x] % 2 > 0)
            {
                oddNumbers[counter] = randomNumbers[x];
                counter++;
            }

        return oddNumbers;
    }
    public static int[] getEvenNumbers(int evenCount, int randomNumbers[])
    {
        int evenNumbers[] = new int[evenCount];
        int counter = 0;

        for(int x = 0; x < evenNumbers.length; x++)
            if(randomNumbers[x] % 2 < 1)
            {
                oddNumbers[counter] = randomNumbers[x];
                counter++;
            }

        return evenNumbers;
    }
}

I'm new to java and have been trying to create a program to generate 100 numbers and sort odds and evens. The program has even giving an .class expected error no matter what I do. Any help would be appreciated.

2
  • Does this even compile? I see compilation errors such as the square brackets on randomNumbers in the calls to getOddNumbers and getEvenNumbers
    – jlars62
    Jul 16, 2013 at 20:15
  • This is not correct if the number can be negative. randomNumbers[y] % 2 > 0 is false for all negative numbers. This is not the opposite randomNumbers[x] % 2 < 1 as x % 2 can return -1 Jul 16, 2013 at 20:16
1

The [] is not needed in this line instead of

int randomNumbers[] = new int[100];
// and 
int oddNumbers[] = getOddNumbers(oddCount, randomNumbers[]);

write

int[] randomNumbers = new int[100];
// and
int[] oddNumbers = getOddNumbers(oddCount, randomNumbers);

The [] is part of the type, not the name.

0

When referring to an array as a whole, don't use the [] with the variable name. Change

int oddNumbers[] = getOddNumbers(oddCount, randomNumbers[]);
int evenNumbers[] = getEvenNumbers(evenCount, randomNumbers[]);

to

int oddNumbers[] = getOddNumbers(oddCount, randomNumbers);
int evenNumbers[] = getEvenNumbers(evenCount, randomNumbers);
0

As Peter Lawrey stated, you don't need the brackets on the function calls. There are also a few other compilation errors. First, it looks like you have a copy/paste error in the getEvenNumbers function. Inside the for loop oddNumbers should be evenNumbers.

You also need to initialize the ints oddCount and evenCount (probably to 0).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.