0

I want to create a print statement that displays three values. 1) Counter variable which shows number of iterations. 2) Array recorder which records the values of the elements and 3) the value of these elements + 5.

There is a change method which takes all of the values in the array and adds 5 to them. I just can't understand how to print this value in line with the counter variable and Array Element counter. Is this possible to do?

int sam[] = {1,2,4,5,6,4,3,67};

change(sam);
for (int y:sam) {
    for(int counter =0; counter<sam.length;counter++) { 
        //this is where I wish to print out the 3 elements
        System.out.println(counter+ "\t\t" + sam[counter]+y);
    }
}

public static void change(int x []) {
    for(int counter=0; counter<x.length;counter++)
     x[counter]+=5;
}
3
  • Can you show what output are you expecting for first element of the array? Is it this: 0 1 6 Commented Mar 1, 2018 at 2:54
  • Yes that is correct Commented Mar 1, 2018 at 3:21
  • Your code iterates 2 times wrapped over the array. Instead you need just the inner loop, since you need the index. There you can perform all the tasks, but from your formulation, it isn't clear to me, what you have to do. I never heard the word 'array recorder', but sounds, like generating a copy. But the +5: Is that only printed or stored back into the array, like J.Doe does? And y needs to be 5, doesn't it? Where did you find that code? Commented Mar 1, 2018 at 3:23

3 Answers 3

1

Everything is fine except that sam[counter] + y is evaluated as integer value because both arguments are integers. You need string concatenation instead:

System.out.println(counter + " " + sam[counter] + " " + y);

Or something like this (using formatter):

System.out.printf("counter = %d, sam[counter] = %d, y = %d\n", counter, sam[counter], y);

%d is a decimal argument, \n is a new line.

EDIT: Regarding your code. If you want to ouput the following row format for each element in the array

counter     sam[counter]        sam[counter] + 5

then just use

int sam[] = {1,2,4,5,6,4,3,67};
for (int counter = 0; counter < sam.length; counter++) {
    System.out.println(counter + "\t\t" + sam[counter] + "\t\t" + (sam[counter] + 5));
}

This will print values in needed format.

0       1       6
1       2       7
2       4       9
...

Or, if you want to change array, but be able to print old values, try this:

int sam[] = {1,2,4,5,6,4,3,67};
for (int counter = 0; counter < sam.length; counter++) {
    System.out.println(counter + "\t\t" + sam[counter] + "\t\t" + (sam[counter] += 5));
}

Here (sam[counter] += 5) will increment each elemnent by 5 and return the new value.

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

1 Comment

That does print 3 lines. The first two colums (counter and array) display the correct values. However the Array + 5 column initializes this value to the first value printed in the array line. E.g. the first print cycle prints a row of 6s, then 7s etc. How can I correct this?
0

Get rid of this outer loop for (int y:sam)

This should work:

for(int counter =0; counter<sam.length;counter++) { 
    System.out.println(counter+ "\t\t" + sam[counter]+ "\t\t" + counter + sam[counter] + 5);
}

2 Comments

This method concatenated the array plus five variable. However the code below works int arrayplus5; for(int counter =0; counter<sam.length;counter++) { arrayplus5 = sam[counter]+5; System.out.println(counter+ "\t\t" + sam[counter]+ "\t\t" +arrayplus5 );
@SamDavis I thought you wanted to concatenate counter+sam[counter]+5
0

Your question was a little hard to interpret, but just drop the outer loop, and don’t “+y”

Scratch that. What do you think the change routine did for you? Did you want an array with the original value and another array with the changed value, and then have access to both of those arrays?

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.