-4

I've been trying to make the following for loops to print the original and modified values of an array in two columns, separated by \t\t. Here is my code:

public class Main {

    public static void main(String[] args) {        

        int jay[] = {1,2,3,4,5,6};
        System.out.println("Original\tAfter");

        for(int y: jay) {
            System.out.println(y);
        }

        multiplyByTen(jay);

        for(int z: jay) {
            System.out.println("\t"+ z);
        }

    }

    public static void multiplyByTen(int x[]) {
        for(int counter = 0; counter<x.length;counter++) {
            x[counter] *= 10;
        }
    }
}

This is the result so far:

Original    After
1
2
3
4
5
6
            10
            20
            30
            40
            50
            60

So my question is how to align the value 10 to 1, and 20 to 2 and so on?

thatguy
  • 21,059
  • 6
  • 30
  • 40
Vu N.
  • 35
  • 9
  • 3
    Why not simply `System.out.println(y + "\t" + 10 * y);`? Without the need of that method, and without looping twice. – Maroun Nov 30 '16 at 16:46
  • 1
    Your function `multiplyByTen` is something I would not use. It changes the argument without returning anything. I would change it so that it returns a *new* array containing the original array multiplied with 10. This way you can use any of the answers below to print both columns using tabs. –  Nov 30 '16 at 16:51
  • This example is from thenewboston java series, I just modified it a bit to see if I can print an original and after arrays side by side. – Vu N. Nov 30 '16 at 17:01

5 Answers5

1

Keep it simple.

Why not simply do

for(int y : jay) {
    System.out.println(y + "\t" + y*10);
}
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

This is the solution of your problem, but i don't know if this is exuctly what you want, but it gives you the wished result

public class Main {

public static void main(String[] args){     

    int jay[] = {1,2,3,4,5,6};
    int jay2[] = jay.clone();


    multiplyByTen(jay);
    for(int i = 0; i < jay.length;i++) {
        System.out.println(jay2[i]"\t"+ jay[i]);
    }

 }
public static void multiplyByTen(int x[]){
    for(int counter = 0; counter<x.length;counter++) {
        x[counter] *= 10;
    }
}
}
BOUALI ALI
  • 230
  • 2
  • 14
1

This example is from thenewboston java series, I just modified it a bit to see if I can print an original and after arrays side by side.

There is no way to print it side by side with your current construct because array gets manipulated and changed when passed into the method. One of the only ways would be making a copy of the original and print both original and after in the same line.


If your multiplyByTen method accepts a single int value you can do it as:

for(int y : jay)
    System.out.println(y + "\t" + mutiplyByTen(y));

If your multiplyByTen method returns an int array, you can do it as:

int[] arr = mutiplyByTen(jay);
for(int x=0; x<jay.length; x++)
        System.out.println(jay[x] + "\t" + arr[x]);

But with the current method signature, you need to make another copy of the original array.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • There is a way. I provided an answer doing exactly that. You just have to do the printing within the same loop. – hfontanez Dec 01 '16 at 15:03
1

My solution using a single array:

public class Main
{
  public static void main(String[] args)
  {
    int jay[] = { 1, 2, 3, 4, 5, 6 };
    System.out.println("Original\tAfter");
    multiplyByTen(jay);

    // To verify values in the original array (you can remove this loop)
    for (int z : jay)
    {
      System.out.println(z);
    }
  }

  public static void multiplyByTen(int x[])
  {
    for (int counter = 0; counter < x.length; counter++)
    {
      System.out.print(x[counter] + "\t\t");
      x[counter] *= 10;
      System.out.println(x[counter]);
    }
  }
}

OUTPUT

Original    After
1           10
2           20
3           30
4           40
5           50
6           60

If you were to use a enhanced loop inside the multiplyByTen(int x[]) method, you would only be changing the local value and not the value in the array. So, if you were to print out the values in the original array, they would remain the same as the original. This way, the values in the array are permanently modified. So, printing the values after the method will show the multiplied values.

Lastly, I would not use print() or println() methods for this. I would use printf() to print out a formatted output. You will find that tabbing will eventually result in misaligned columns (when the number of digits gets larger). You would not run into this issue when using printf().

hfontanez
  • 5,774
  • 2
  • 25
  • 37
  • 1
    Yesterday I wrote a similar answer, but I deleted it half way through since OP is only experimenting with his "own design" by tweaking the method signature. The reason I deleted a similar answer as yours is because by design, we should not even do the printing in a method which is only supposed to do multiplication. This encourages tight coupling with low cohesion. A method should only do a specific task at a time. Hence, instead of writing this, I suggested other method signature alternatives. – user3437460 Dec 01 '16 at 17:11
  • @user3437460 This is true. "Printing out" should not be done at all. This is normally done so that people learning could check results visually. You could, replace the "System.out.print" with logging and it should be perfectly OK. I wasn't going to post an answer, but I did so because there is absolutely no reason to use two arrays. The answer should only use a single array that 1) stayed unmodified and only print the value of each index x10, or 2) multiply the current value x10 and store the result in the same index. In either case, using two arrays is really the wrong answer. – hfontanez Dec 01 '16 at 20:59
-1

It is far better to use Arrays.copyOf(...) when you think of using .clone()1:

int jay[] = {1,2,3,4,5,6};
int jay2[] = Arrays.copyOf(jay,jay.length);
System.out.println("Original\tAfter");
multiplyByTen(jay2);
for (int i = 0; i < jay.length; i++) {
    System.out.println(jay[i]+"\t\t"+jay2[i]);
}

This way, you print table rows and not columns.

1Why you should never use .clone() for defensive copying.

ItamarG3
  • 4,092
  • 6
  • 31
  • 44