Aod Ren
Aod Ren

Reputation: 679

Repeat an integer n times

I'm trying to make a pyramid out of an integer. I.E the number 3 :

3
33
333

So based on the answers i found i made this :

int n = 8;
String n2 = Integer.toString(n);

for (int i=0; i<n; i++) {
    System.out.println(StringUtils.repeat(n2, i));
}

But it's not working and would be suboptimal. Is there a simple way to repeat an integer n times in the same line ?

EDIT : made myself a method.. not quite happy either but it seems i can't just use something like System.out.println(int x, int n times)

int n = 8;

for (int i=0; i<=n; i++) {
    for (int j=0; j<i; j++) {
        System.out.print(n + " ");
    }
    System.out.println("");
}

Upvotes: 3

Views: 21557

Answers (5)

Andremoniy
Andremoniy

Reputation: 34900

Ok, you can do this without explicit loops using Java-8 streams:

IntStream.range(1,n).forEach(i -> System.out.println(StringUtils.repeat(n2, i));

or even without apache-commons:

 IntStream.range(0,n).forEach(i -> System.out.println(String.join("", Collections.nCopies(i+1, n2))));

But in any case internally all these methods use loops.

Upvotes: 7

Arnaud
Arnaud

Reputation: 17534

To keep with integers, you may store last value each time, and add the next part :

i = 0 --> you get 3

i = 1 --> you get 33 (i0 + 30)

i = 2 --> you get 333 (i1 + 300)

int lastValue = 0;

for (int i=0; i<=n; i++) {

  int currentValue  = lastValue + (n * Math.pow(10, i));
  System.out.println(currentValue);
  lastValue = currentValue ;
}

This obviously works for one-digit integers only.

Upvotes: 0

Erik Nystr&#246;m
Erik Nystr&#246;m

Reputation: 547

You could try to use a StringBuilder.

You would still have to loop, but it might be slightly better performance-wise.

int n = 8;
String n2 = Integer.toString(n);
StringBuilder builder = new StringBuilder(n);
for(int i = 0; i < n; i++) {
    builder.append(n2);
    System.out.println(builder.toString());
}

This does what you want, but not in the way you think about it. Instead of repeatedly having to create the repeating integer string, we simply build ONE string, saving us the work of repeating it.

To actually answer your question, you could use this code, although I would recomend the first approach:

char[] str = new char[n];
Arrays.fill(str, (char)(number + '0'));
new String(str);

This would only work if your integer is 0 <= number < 10.

Upvotes: 0

Meera M Babu
Meera M Babu

Reputation: 68

I mean isn't it suboptimal to convert my int into a string ? AIn't there a direct way to deal with the integer ? –

If you dont want to convert int to string.

This may help you.

    int n = 3;
    for (int i=1; i<=n; i++) {
    System.out.println(new String(new char[i]).replace("\0", n+""));
    }

Upvotes: 0

Ankur Singhal
Ankur Singhal

Reputation: 26077

Something as below.

public class Test{

    public static String repeat(String str, int times) {
        return new String(new char[times]).replace("\0", str);
    }

    public static void main(String[] args) {
        for (int i = 1; i < 5; i++) {
            System.out.println(repeat("3", i));
        }
    }
}

Output

3
33
333
3333

Upvotes: 0

Related Questions