0

Hi everyone,
Could anyone please tell me why that line in the if-statement body is breaking the method?

static int [] doubleMe(int [] list){
    	int [] y= list;
    	int c = 0;
    	int [] w;
		for (int i= 0; i < list.length; i++){
    		 c = list[i];
			if (c % 2 == 0)
				w = c * 3;
		}
		return w;
    }

Thanks..

2
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by rotten69
0

w is an array of ints, c (and c * 3) are ints.
you should set c * 3 to an element of w, not to w itself, or you should declare w as an int

0

yeah but What I have in my program is

int [] a = {1,2,3}
int [] b = doubleMe(a);

But this won't work if I have w as just int and the return value is int.

Edited by rotten69: n/a

0

ehm ... don't know what you mean by that, but what I ment was:

if (c % 2 == 0)
    w = c * 3;

this is actually like saying:

int[] w = 5;

w is an array, so, if you want to set the value for w, you'll need to pass an array, not an int. or, you need to set c * 3 to an element of w, like

w[i] = c * 3;

but, before you do this, you'll need to initialize the array. at this point, you have only declared it.

0

Thanks buddy. This seems to be working. it's a simple mistake that ruins a whole idea..

This question has already been answered. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.