Can someone tell me what is wrong with this code? Please!

0 Tushar A · April 11, 2015
package bucky;

public class apples {
public static void main(String args[]){
int bucky[]={2,4,5,6};
change(bucky);

for(int x:bucky)

System.out.println(x);
}

public static void change(int y[])
{
for(int z:y)
z+=5;
System.out.println(z);
}
}

Post a Reply

Replies

Oldest  Newest  Rating
0 steven kitchener · April 12, 2015
Im not really sure what you are trying to accomplish here but ill give it a go. In future though, please expand on what you are trying to do and what kind of errors you are getting.

Im going to assume that you are trying to take your original int array of 2,4,5,6 and add 5 to all the values to get an expected outcome of 7,9,10,11. If this isnt what you are looking for then please comment below what it is you are actually trying to achieve.

Right, first off i'd like to change the loop that you are using in the static method so that we can keep track of the amount of iterations we have done (how many times its looped). If you are unsure of how to use this kind of loop then again comment below and ill talk you through it a little more in depth. Anyway this is what its going to look like:
for(int i = 0; i < y.length;i++){

}

The body of the loop is only going to be 2 lines long and they are going to be:
y += 5;                  /*or if you dont like shorthand code*/ y = y + 5;
System.out.println(y) ;

Once you make the changes the output will look like 7,9,10,11,7,9,10,11 (on separate lines). You dont even need the second line in the loop as the second loop in the main class prints the exact same thing.

Hope this helps

EDIT: so for some reason square brackets dont appear in the code, so for the two statements above anywhere there is a "y" it should be "y_openSquareBracket_i_closeSquareBracket"
0 steven kitchener · April 12, 2015
Okay so it looks like ive overcomplicated this massively due to misreading the code haha, oops.

The reason your code isnt working is because you are missing a set of braces in the loop. If you dont have braces in loops( or if/else statements for that matter) then Java will only run the next statement inside the loop, in this case the z+=5, which does nothing and when it comes time to print out "z" it cant find the variable as technically it has never existed outside that loop. 

To remedy this just add braces like in the code below 

public static void change(int y[])
{
for(int z:y){
z+=5;
System.out.println(z);
}
}

The inclusion of braces makes the loop run over all the statements inside the braces. 
Sorry if i confused you further with my first comment =]
Steven
0 Jeffrey Duarte · April 12, 2015
Steven's update is correct. You utilized a for-in loop which is perfectly fine, but you missed the opening bracket for the loop in change (reference Steven's comment for the fix.) There is also a pair of brackets missing for the for-in loop in the main method:
public static void main(String args[]) {
    int bucky[] = {2,4,5,6};
    change(bucky);


    for (int x: bucky) {
        System.out.println(x);
    }
}


Hope this solves the issue.
0 Tushar A · April 12, 2015
Thanks a lot Steven and Jeffrey.

Just one issue. Try running the corrected code and the output is like 7 9 10 11 and 2 4 5 6. Does the actual bucky array does not change after I have called the change method. Looks like only a copy of the values of the array is sent into the change method and not the actual values?
0 Jeffrey Duarte · April 12, 2015
Hi Tushar.

The change method in this example doesn't actually change the array content, it just iterates through it. In order to actually change the content I would make use of a standard for loop. This allows us to keep track of the iteration/index of the array we are working on. This is important because we need to reference that index in order to make any permanent changes. It would look like this:
public static void change(int[] y) {
    for (int i = 0; i < y.length; i++) {
        y[i] += 5;
        System.out.println(y[i]);
    }
  }

In the for-in loop we're creating a new instance of int z with each iteration and printing it to the screen without storing it anywhere, hence the nonpermanent changes.
0 steven kitchener · April 12, 2015
If you want to change the values, see my first comment 
0 Jeffrey Duarte · April 12, 2015
Hey Steven. I would have referenced your answer but there was one issue I saw. There wasn't any reference to an index. I didn't try running your code but I get the feeling it would say something about types not matching at the following line:
y += 5;

The reason being that this means you want an array (y) to be equal to an integer (5). The second time is a logical error the compiler wouldn't catch:
System.out.println(y);

would print out the complete array every time the loop runs. The result would look something like:
{7, 4, 5, 6}
{7, 9, 5, 6}
{7, 9, 10, 6}
{7, 9, 10, 11}
7
9
10
11

I may be wrong though, I'm still learning myself. Please, let me know if your version of the code works as well.
0 Jeffrey Duarte · April 12, 2015
Sorry Steven. I didn't see your edit.
  • 1

Java / Android Development

107,173 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator