Home > database >  How to print the same array twice but horizontally? - Java
How to print the same array twice but horizontally? - Java

Time:04-09

I'm having a hard time getting this to print the way my professor wants. I have tried print, println, \t, and, \n however the results either come out vertically and side by side or each duplicated number next to each other horizontally. This is what my professor wants:

The results (printed twice) are:
14 81 94 91 47 57 98 89 82 15 
14 81 94 91 47 57 98 89 82 15

my outputs have consisted of:

The results (printed twice) are:
14 14
81 81
94 94
91 91
47 47
57 57
98 98
89 89
82 82
15 15
The results (printed twice) are:
14 14 81 81 94 94 91 91 47 47 57 57 98 98 89 89 82 82 15 15
The results (printed twice) are:
14      14 81   81 94   94 91   91 47   47 57   57 98   98 89   89 82   82 15   15

This is my code thus far:

System.out.println("The results (printed twice) are:\t");
        for (i = 0; i< arr.length; i  ){
            arr[i] = rand.nextInt(100)   1;
            int[] arrb = arr.clone();
        System.out.print(arr[i]   " "   arrb[i]   " ");

CodePudding user response:

Your current code attempts to do 2 things in a single loop:

  • Generate a new random number.
  • Print it (twice).

This is never going to work - you need to print the other numbers first, and then 'start over' and print the first number a second time. You can't ask java to 'hop between lines' - standard output is a rather abstract concept. Perhaps its a line printer; you can't ask it to swallow a page it just printed out back into itself and unprint something, for example.

Thus, the answer lies in separating out your tasks. This is usually a good idea for any programming task of any stripe:

  • Fill an int[] array by generating random numbers.
  • Print an int[] array - whatever may be inside it.
  • Do the previous thing twice in a row.

That involves at least 3 for loops, in other words.

int LEN = 100;

int[] numbers = new int[LEN];
for (int i = 0; i < LEN; i  ) {
  numbers[i] = rnd.nextInt(100)   1;
}

That's one job done. Don't complicate matters by attempting to also print from there. You're just doing the one job.

Printing one line is easy:

for (int i = 0; i < LEN; i  ) {
  System.out.print(numbers[i]   " ");
}
System.out.println();

And how do we do things more than once? With.. for loops:

for (int print = 0; print < 2; print  ) {
  for (int i = 0; i < LEN; i  ) {
    System.out.print(numbers[i]   " ");
  }
  System.out.println();
}

CodePudding user response:

Something like this could work:

int[] arr = new int[] { 14,81,94,91,47,57,98,89,82,15 };

    System.out.println("The results (printed twice) are:\t");

    int counter = 0;

    while(counter <= 1) {

        for (int i = 0; i< arr.length; i  ) {
            System.out.print(arr[i]   " ") ;
        }
        counter  ;
        System.out.println();
    }

Or if you want a nested for loop could also do the work:

int[] arr = new int[] { 14,81,94,91,47,57,98,89,82,15 };

    System.out.println("The results (printed twice) are:\t");

    for (int i = 0; i <= 1 ; i  ){

        for (int j = 0; j < arr.length; j  ) {
            System.out.print(arr[j]   " ") ;
        }
        System.out.println();
    }

And then the output should be:

The results (printed twice) are:
14 81 94 91 47 57 98 89 82 15 
14 81 94 91 47 57 98 89 82 15

Basically the logic here is just repeat the printing of the list two times with another for loop or while loop, both works the same way in the examples above, if you have any question you can reply my answer.

CodePudding user response:

Use something like this instead:

System.out.println("The results (printed twice) are:\t");    
for (i = 0; i< arr.length; i  ){
    arr[i] = rand.nextInt(100)   1;
int[] arrb = arr.clone();

System.out.print(Arrays.toString(arr));
System.out.println();
System.out.print(Arrays.toString(arrb));

Example output:

[1,2,3,4]
[1,2,3,4]

If you want the outout to be 1 2 3 4 instead, use a simple for-loop

for (int i = 0; i<arr.lenght; i  ){
    System.out.print(arr[i]   " ")
}
System.out.println();
for (int i = 0; i<arrb.lenght; i  ){
    System.out.print(arrb[i]   " ")
}

CodePudding user response:

There is no need to clone your array.

    int[] arr = new int[10];
    Random rand = new Random();

    System.out.println("The results (printed twice) are:\t");
    for (int i = 0; i < arr.length; i  ) {
        arr[i] = rand.nextInt(100)   1;
        System.out.print(arr[i]   " "   arr[i]   " ");
        System.out.println();
    }

The result:

 The results (printed twice) are:    
 58 58 
 31 31 
 22 22 
 81 81 
 36 36 
 38 38 
 40 40 
 9 9 
 10 10 
 11 11 

CodePudding user response:

Did u try to System.out.println ??? Something like that:

System.out.println("The results (printed twice) are: ");
        for (i = 0; i< arr.length; i  ){
            arr[i] = rand.nextInt(100)   1;
            int[] arrb = arr.clone();
            System.out.println(arr[i]);  
            System.out.println(arrb[i]);        
}

Or u can try to make 2D array and fill row[0] with Your arr.
Then fill row [1] with Your arrb

  •  Tags:  
  • java
  • Related