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..

Recommended Answers

All 7 Replies

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

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.

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.

Sorry. I missed the variable w in my previous comment..

do you still have the problem, or have you been able to solve it?

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

It's solved and the thread is marked as solved too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.