Hey there. I am trying to return an array, but the compiler is telling me that it's an incompatible type..?
My code: http://pastie.org/private/v4xn4eahkojftsghudf1g
I'm a beginner at Java, sorry about this :)
Thanks very much.
Hey there. I am trying to return an array, but the compiler is telling me that it's an incompatible type..?
My code: http://pastie.org/private/v4xn4eahkojftsghudf1g
I'm a beginner at Java, sorry about this :)
Thanks very much.
im guessing you're talking about
static int getOddList(int[] a);
the return type is set to int
, should be int[]
ie:
static int[] getOddList(int[] a)
it's the same with the other functions you're returning arrays. needs to be int[]
, not int
For the following methods:
static int getOddList(int[] a)
static int getEvenList(int[] a)
You've declared the methods as returning single integer values although in the code for the methods you're actually returning:
return oddList;
return evenList;
Both of those variables are declared as int[] xxxxxx = new int[10];
You'll want to change the method signatures for both getOddList and getEvenList to:
static int[] getOddList(int[] a)
static int[] getEvenList(int[] a)
One question I do have to ask is in your main
method, you're just calling the getXXXX
methods without storing the results.
// Create and print arrays from input
getNegativeList(input);
getOddList(input);
getEvenList(input);
If you're not going to be storing the results of the method call, it may make more sense to declare the method signatures of getOddList
and getEvenList
as:
static void getOddList(int[] a)
static void getEvenList(int[] a)
Then, you'd update the code for the method not to return any values.
Again, that's just a suggestion. If you are planning on storing the results of those methods but just haven't got around to writing that code yet then you should be fine just changing int
to int[]
for the return types of the two mentioned methods.
And before you ask: you should probably declare your total variables outside your for loop, if you're intending to keep just the odd numbers, etc.