-5
import java.util.Scanner;
import java.util.Arrays;

/**
   This class prints the numeric value of a letter grade given by the user.
*/
public class Numbers
{

int countEven=0;
int countOdd=0;
private int[] digits;
private int[] evenoddCount;
Scanner input = new Scanner(System.in);

/**
    Constructs numbers set to 0
    @param anEfficiency the fuel efficiency of the car
*/
public Numbers()
{
    digits = new int[10];
}

/**
    collects 10 numbers from user and places then into array
    @return the gradeValue
*/
public void inputArray(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
{
    digits = {a, b, c, d, e, f, g, h, i, j};
}

 /**
    counts even and odds
    @return numeric grade
 */
public void evenOdds()
{
    for(int i=0; i < digits.length; i++)
    {
        if(digits[i]%2 == 0)
            countEven++;
        else
            countOdd++;
        evenoddCount = {countEven, countOdd};
    }
}

 /**
    prints out the array of 10 positive integers
    @return numeric grade
 */
public void printArray()
{
    System.out.println(Arrays.toString(evenoddCount));
}



}






}

Code is supposed to find even odd numbers using arrays. Comments are messed up though for some reason. I seem to have messed up near line 31 which has pretty much messed up the rest of the program for some reason? I think I messed up my declarations.

  • 1
    Now tell us why do you think it is not working correctly – Baby Dec 05 '14 at 03:43
  • 1
    If you could describe the problem you're seeing - that is, what result you're getting, compared with the result you expected - that would make it a lot easier for us to help you. – Tim Dec 05 '14 at 03:43
  • You never initialise `evenoddCount` for one thing: `evenoddCount = new int[2];`. – AntonH Dec 05 '14 at 03:44
  • @javaProgrammer try this: http://stackoverflow.com/questions/1938101/how-to-initialize-an-array-in-java – Dinal24 Dec 05 '14 at 03:52

1 Answers1

1

I believe the problem is when you assign to the array. You are doing this:

digits = {a, b, c, d, e, f, g, h, i, j };

When you should be doing this:

digits = new int[] {a, b, c, d, e, f, g, h, i, j};

Also, instead of assigning a new array to evenOddCount every time, just increment in the loop, like this:

for(int i=0; i < digits.length; i++)
{
    if(digits[i]%2 == 0)
        evenoddCount[0]++;
    else
        evenoddCount[1]++;
}

And when you declare your evenOddCount array, declare it like so:

int[] evenOddCount = {0,0};
Shadow
  • 3,926
  • 5
  • 20
  • 41