0

Let me explain. Say I have a given array of integers:

public static void spacing(String str)
    int[] spacing = new int[]{1, 2, 3, 2, 1};

     //Some function to find the given number in 
     //the array and coverts to the equal number of spaces

    for (int i = 0; i < str.length(); i++)
        System.out.println((the method for spacing) + str.charAt(i));

public static void main(String[] args)
    spacing("Wow!!");

I understand it is not formatted properly. I just copied the gist over so you can get an idea of what I am wondering. How can I convert those given integers into the same number of spaces? (Meaning 1 would print 1 space, 2 would print 2 spaces, 3 spaces, etc...) Is it possible to print the space character spacing[i] times?

Edited: I am limited to only importing java.utils.Arrays java.lang I have also updated the text to show how it needs to produce a pattern. I have also tried some of the answer provided, however it is still not printing the correct number of spaces.

I have tried converting the int array to a char array and using Array.fill and while it fills the array with spaces, it's only one space per array value (instead of 2 having 2 spaces, it puts 1 space for each value). My program is supposed to take characters from the user (which it already does) I am just having trouble getting the array to print the proper number of spaces before each character.

This is how the expected result looks if the user enters "Wow!!":

 W
  o 
   w
  !
 !
  • 1
    Possible duplicate of [Simple way to repeat a String in java](https://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string-in-java) – racraman Oct 16 '19 at 23:18
  • I am not trying to repeat strings, rather place a given number of spaces before each character within an user inputted string – SillLearning Oct 17 '19 at 01:46

2 Answers2

0

You can do something like this:

public static void main(String[] args)
{
    String space = " ";
    int[] spacing = new int[]{1, 2, 3, 4, 5};
    for (int n: spacing) {
        System.out.print(String.join("", Collections.nCopies(n, space)));
        System.out.println("End of line");
    }
}

I've added the "End of line" just for example purposes

Villat
  • 1,455
  • 1
  • 16
  • 33
0

You can create a method and pass integer as argument. append string with spaces and loop it based on passed integer and return string of spaces like this

public static void main(String args[]) {
    String str = "Wow!!";
    int[] spacing = new int[]{1, 2, 3, 2, 1};
    for (int i = 0; i < str.length(); i++) {
        System.out.println(method2(i) + str.charAt(i));
    }
}
static String method2(int n) {
    String space = "";
    for (int i = 1; i <= n; i++) {
        space = space + " ";
    }
    return space;
}

call this method before printing number or character it.

flashleo
  • 45
  • 6
  • I appreciate all the help you've been giving me! I see what you're saying now about the spacing now, but it still does not implement the given array. I have tried a couple different ways like having a nested for loop that changes the value of `n` to the `int array` but it gets rid of the incrementing spacing and just prints a straight line. Every other way I try other than that just gives a compilation error. This is something I have been struggling with and cannot figure it out :( – SillLearning Oct 17 '19 at 02:31