Home > front end >  how do i change my output from horizontal to vertical in java?
how do i change my output from horizontal to vertical in java?

Time:01-08

I need to write a function that takes an array and prints '*' for each index by the value of the index for example for 1,2,3,4 the output will look like this: enter image description here but my output is vertical

  • 1
    • 2
      • 3
        • 4

this is my printing code :

    public static void printStars(int[] a) {
        for (int i = 0; i < a.length; i  ) {
            for (int j = 1; j <= a[i]; j  )
                System.out.print("*");
            System.out.println(" "   a[i]);
        }
    }

CodePudding user response:

[edit] try the following:

public static void printStars(int[] a) {
    int maxValue = Collections.max(Arrays.stream(a).boxed().toList());
    String[] line = new String[a.length];  //this also works
    
    for (int i = maxValue; i >=0 ; i--) {
        //String[] line = new String[a.length];  //this will keep allocating new memory
        for (int j = 0; j < a.length; j  ) {
            if (i == 0) {
                line[j] = String.valueOf(j 1); //<change j 1 to a[j] in order to print out the value at each index of the array if you are not looking to print indexes
            }else if (a[j] >= i) {
                line[j] = "*";
            }else {
                line[j] = " ";
            }
        }
        System.out.println(String.join(" ", line));
    }
}

it takes the maximum value in the array and stores in a variable. This is used to iterate each line. After that, it checks if at this current iteration, does an index of your array contain an asterisk in this location? if yes, assign asterisk to the specific index of the string array corresponding to index of original array, else assign whitespace.

Finally, when it goes to 0, you assign the either the values of your array or the indexes of the array to the string[]. Then you print the array by using String.join() with a delimiter of whitespace. This allows you to focus on white index contains a whitespace or not, and not need to focus on the formatting of whitespaces in between each item.

for the input [1,2,3,4] output is:

      *
    * *
  * * *
* * * *
1 2 3 4

for the input [1,7,3,4]:

  *    
  *    
  *    
  *   *
  * * *
  * * *
* * * *
1 2 3 4

CodePudding user response:

The solution from the previous answer works but I provided a slightly more compact version printStars and renamed the old one to printStarsOld. Here is the code:

import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class IntTest {

    public static void printStarsOld(int[] a) {
        int maxValue = Collections.max(Arrays.stream(a).boxed().toList());
        String[] line = new String[a.length];  //this also works

        for (int i = maxValue; i >= 0; i--) {
            for (int j = 0; j < a.length; j  ) {
                if (i == 0) {
                    line[j] = String.valueOf(j   1);
                } else if (a[j] >= i) {
                    line[j] = "*";
                } else {
                    line[j] = " ";
                }
            }
            System.out.println(String.join(" ", line));
        }
    }

    public static void printStars(int[] a) {
        List<Integer> list = Arrays.stream(a).boxed().toList();
        StringBuffer string = new StringBuffer();
        Integer max = list.stream().max(Integer::compare).get();

        for (int i = max; i > 0; i--) {
            int finalI = i;
            list.forEach(integer -> string.append(integer - finalI < 0 ? ' ' : '*').append(' '));
            System.out.println(string.toString());
            string.setLength(0);
        }
        for (Integer i=1; i<=list.size();i  ) System.out.print(i.toString()   ' ');
    }

    @Test
    public void test() {
        System.out.println("Old output: ");
        printStarsOld(new int[]{2, 4, 5, 1, 3});
        System.out.println("New output: ");
        printStars(new int[]{2, 4, 5, 1, 3});
    }
}

The output is:

Old output: 
    *    
  * *    
  * *   *
* * *   *
* * * * *
1 2 3 4 5
New output: 
    *     
  * *     
  * *   * 
* * *   * 
* * * * * 
1 2 3 4 5 
  • Related