Adding space in Java

86 Views Asked by At

So I am trying to practice figure out the quickest and easiest way to add spaces in between the character values.

public static void main(String[] args) {      
        char arr[] = {'g', 'a', 'p', 'e', 't'};
        
        System.out.println("Array: " + new String(arr)); //expected g a p e t
                                                         //instead I got gapet
    }

I tried converting the char values as a string, but I still can't figure out to add spaces in between each letter.

6

There are 6 best solutions below

0
Reilas On BEST ANSWER

There are a few approaches.

The simplest would be to use a for-loop.

String string = "";
int index = 0;
for (char character : arr) {
    if (index++ != 0) string += " ";
    string += character;
}

Or, as others have mentioned, you can use the String#split method, and then use the String#join method.

String string = String.join(" ", new String(arr).split(""));

You could, also, use a StringBuilder, utilizing the insert method.

StringBuilder string = new StringBuilder(new String(arr));
for (int index = string.length() - 1; index >= 0; index--)
    string.insert(index, ' ');

Finally, you could use, with caution, a String#replace.
Note that, this will place an additional space character at the beginning, and end.
You can use, String#trim to remove them.

String string = new String(arr).replace("", " ").trim();
0
Derek On

This would be the approach I would take

public static void main(String[] args) {
    char arr[] = {'g', 'a', 'p', 'e', 't'};

    // Convert the char array to a string with spaces in between
    String result = String.join(" ", new String(arr).split(""));

    System.out.println("Array: " + result); // Output: g a p e t
}
0
David Hash On

You can loop through the array of characters as follows

public static void main(String[] args) 
{
    char arr[] = {'g', 'a', 'p', 'e', 't'};

    for (char i : arr)
    {
      System.out.print(i + " ");
    }
  }
}
0
GeRmAnImAl On

Let the StringJoiner do the work for you:

public static void main(String[] args) {
    char arr[] = {'g', 'a', 'p', 'e', 't'};
    StringJoiner joiner = new StringJoiner(" ");
    for (char c : arr) {
        joiner.add(String.valueOf(c));
    }
    System.out.println(joiner.toString());
}
0
a.sh On

Here's an implementation using basic Java. I've added a separate print statement at end to avoid space at the end of array.

public static void main(String[] args) 
{
    char arr[] = {'g', 'a', 'p', 'e', 't'};

    for (int i=0; i<arr.size()-1; i++)
    {
      System.out.print(arr[i] + " ");
    }
    System.out.print(arr[arr.size()-1]);
  }
}
0
Salah Mohammed On

After the loop, the trim() method is called on the resulting string to remove any trailing whitespace. Finally, the string is printed, resulting in the output: "Array: g a p e t".

public static void main(String[] args) {
    char arr[] = { 'g', 'a', 'p', 'e', 't' };

    StringBuilder sb = new StringBuilder();
    for (char c : arr) {
        sb.append(c).append(" ");
    }
    String result = sb.toString().trim();

    System.out.println("Array: " + result);
}