1

I want to return odd numbers of an array yet Eclipse doesn't seem to accept my return array[i]; code. I think it requires returning a whole array since I set an array as a parameter to my method. As I said before, I need to pass an array and get a specific element of that array in return. Even if I make that array static, how do I return a single element?

Edit : Alright then, here it is:

public class newClass{
public static void main(String[] args) 
{       
    int [] newArray= new int [4];
    int [] array = {4,5,6,7};

    newArray[0] = array[0]+array[1]+array[2]+array[3];
    newArray[1] = array[0]*array[1]*array[2]*array[3];
    newArray[2] = findOut(array);

}

public static int findOut (int [] array3)
{
    int e1=0;
    int e2=0;
    for (int i=0; i<array3.length; i++)
    {
        if (array3[i]%2==0)
        {
            e1+=array3[i];
            array3[i]=e1
            return array3[i];
        }

        else
        {
            e2+=array3[i];
            array3[i]=e2;
            return array3[i];

        }

    }

}


}

I know there are probably more than a few mistakes here but I'm working on it and I'm not only returning odd numbers, I also add them together.

5
  • 12
    Please show us the code Commented Oct 15, 2013 at 8:42
  • 1
    if you want to return odd numbers, you have to return a array of numbers instead of a single number, or did I misunderstood your question? if you have an input array of numbers and you want to return all the odd numbers, you have to return an array too. e.g. for input [1, 2, 3, 4, 5], you will return [1, 3, 5], right? Commented Oct 15, 2013 at 8:43
  • 3
    When asking such questions, provide the relevant code and the exact compilation error. It will make your answer much clearer and you will get better answers. Commented Oct 15, 2013 at 8:44
  • It's fine if the code doesn't work. It's not fine to leave it out. :-) If you include it, we can more usefully help you by telling you exactly where things are going wrong. Seeing what you've done wrong points the way toward helping you best. Commented Oct 15, 2013 at 8:56
  • 1
    It's not at all clear what you want. You said you want one element, but you also want odd "numbers" plural and have apparently declared an array return type. If you do want just one element, the first element matching the rule, or some later one? Do you mean odd array indexes or odd numbers at any index? An array of what type? Post the code or you're impossible to help. Commented Oct 15, 2013 at 8:57

4 Answers 4

12

You code should look like this:

public int getElement(int[] arrayOfInts, int index) {
    return arrayOfInts[index];
}

Main points here are method return type, it should match with array elements type and if you are working from main() - this method must be static also.

Sign up to request clarification or add additional context in comments.

Comments

3

I want to return odd numbers of an array

If i read that correctly, you want something like this?

List<Integer> getOddNumbers(int[] integers) {
  List<Integer> oddNumbers = new ArrayList<Integer>();
  for (int i : integers)
    if (i % 2 != 0)
      oddNumbers.add(i);
  return oddNumbers;
}

Comments

1

Make sure return type of you method is same what you want to return. Eg: `

  public int get(int[] r)
  {
     return r[0];
  }

`

Note : return type is int, not int[], so it is able to return int.

In general, prototype can be

public Type get(Type[] array, int index)
{
    return array[index];
}

Comments

0

(Edited.) There are two reasons why it doesn't compile: You're missing a semi-colon at the end of this statement:

array3[i]=e1

Also the findOut method doesn't return any value if the array length is 0. Adding a return 0; at the end of the method will make it compile. I've no idea if that will make it do what you want though, as I've no idea what you want it to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.