TransWikia.com

How to format this output?

Stack Overflow Asked by Shantanu Jain on August 6, 2020

I have created a method, which is supposed to return a growing number list when entered any number.
For example:

input: 5

expected output: [1, 2 2, 3 3 3, 4 4 4 4, 5 5 5 5 5]

current output : [1 , 2 2 , 3 3 3 , 4 4 4 4 , 5 5 5 5 5 ]

input: 2

expected output: [1, 2 2]

current output : [1 , 2 2 ]

How do I delete the white spaces?
This is my code:

ArrayList < String > maxarray = new ArrayList < String > ();
int i, j;
String num = "";

for (i = 1; i <= max; i++) {
    num = "";
    for (j = 1; j <= i; j++) {
        num = num + i + " ";
    }
    //System.out.print(i);
    maxarray.add(num);
}
return maxarray;

I have tried: num= num.replace(" ","");
num = num.replace(" ", "");
but, they don’t seem work.
and if I try to convert it into a string, I get the following output: 1 2 2 3 3 3

Help me, please

2 Answers

Easy and short-sighted solution: just trim the string before adding it to the ArrayList:

        maxarray.add(num.trim());

With this change the output is:

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

trim() removes trailing spaces (and leading, had there been any), but not the spaces between the numbers.

Produce correct output from the outset: However, rather than producing not-quite-right output and then correcting it it’s probably less confusing in the end to produce correct output from the outset. That is, without the space after the last number. This is also what the answer by Berto99 does. My preferred way of doing this is: take one iteration out of the inner loop and refrain from adding the space there.

    for (i = 1; i <= max; i++) {
        num = "";
        for (j = 1; j < i; j++) { // stop one number before i
            num = num + i + " ";
        }
        num = num + i; // add no space here
        maxarray.add(num);
    }

Output still is:

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

I find this way easier to read than the version with the extra if statement in the answer by Berto99.

For the sake of precision: maxarray is not an int array, it as an ArrayList of strings. And what we needed to trim was not the ArrayList but each string in it.

PS Stream version: For readers who know and like streams here’s a stream version. If you don’t want to learn about streams yet, just ignore.

    List<String> maxarray = IntStream.rangeClosed(1, max)
            .mapToObj(i -> IntStream.rangeClosed(1, i)
                    .mapToObj(j -> String.valueOf(i))
                    .collect(Collectors.joining(" ")))
            .collect(Collectors.toList());

One of the convenient things is that Collectors.joining(" ") puts spaces between the numbers without putting any space before the first or after the last, which solves the problem asked about.

Answered by Ole V.V. on August 6, 2020

As you can see from here:

ArrayList < String > maxarray = new ArrayList < String > ();
int i, j;
String num = "";

for (i = 1; i <= max; i++) {
    num = "";
    for (j = 1; j <= i; j++) {
        num = num + i + " ";
                        ^^^^-- here
    }
    //System.out.print(i);
    maxarray.add(num);
}
return maxarray;

you add a space after every number, so you only need to check if you are not in the last number, in order not to add the space in that case:

ArrayList < String > maxarray = new ArrayList < String > ();
int i, j;
String num = "";

for (i = 1; i <= max; i++) {
    num = "";
    for (j = 1; j <= i; j++) {
        num = num + i;
        if(j < i) // <--- check if you are not in the last iteration
           num = num + " ";
    }
    //System.out.print(i);
    maxarray.add(num);
}
return maxarray;

Answered by Berto99 on August 6, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP