2

I want to initialize a string as follows:

public int function (  int count )  { 
String s_new = "88888...  'count' number of 8's "    <-- How to do this
//Other code 
}

Currently I'm not sure how to do this, so I've declared an int array ( int[] s_new ) instead and Im using for loops to initialize this int array.

EDIT: I meant that I need to initialize a string containing only 8's ... the number of times the digit 8 occurs is 'count' times.

Nikhil
  • 797
  • 6
  • 12
  • 30
  • Is the `88888...` part of the string supposed to represent the value of `count`? ie the user passes in 5, you put 5 eights there? – Durandal Jan 18 '14 at 20:28

5 Answers5

4

You can use Guava's Strings.repeat() method:

String str = Strings.repeat("8", count);
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
3

Try:

String s_new = "";
for (int i = 0; i < count; i++) {
    s_new += "8";
}
return s_new;

Now, this is a naive solution. A better solution (as is posted in other answers here) will use a StringBuffer or StringBuilder to accomplish this in a more efficient manner.

Also, further reading on the difference between those two options: Difference between StringBuilder and StringBuffer

Community
  • 1
  • 1
BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
3

In these cases, it is recommended to use a StringBuilder:

StringBuilder sb = new StringBuilder();
String s = "";
int count = 8;

for (int i = 0; i < count; i++) {
    sb.append('8');
}

s = sb.toString();

System.out.println(s);

Output:

88888888
Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • This is a more efficient way to accomplish this than mine (memory/time). +1 – BlackVegetable Jan 18 '14 at 20:31
  • Java has a pretty smart compiler, all string `+` operations are actually converted to `StringBuilder.append's` so I doubt it, but when dealing with 8 characters output you need to be sure to optimize everything – Philipp Gayret Jan 18 '14 at 20:32
  • @Philipp I was unaware of how the Java compiler handled such. But, yeah. Those 8 character outputs could take a mighty long time. ;) – BlackVegetable Jan 18 '14 at 20:34
  • 1
    @Philipp `+` is internally implemented via `StringBuilder` but the compiler will do that in a loop here. `new StringBuilder(s).append('8').toString()` each time. You have move the builder out of the loop manually – zapl Jan 18 '14 at 20:36
  • @Philipp not in loops I believe and this isn't part of the Java spec so cannot be relied upon in non-Oracle compilers. – Boris the Spider Jan 18 '14 at 20:36
2

You can build strings using the StringBuilder class.

StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++)
    sb.append('8')
String s_new = sb.toString();

s_new would then have as much 8 as you have count.

zapl
  • 63,179
  • 10
  • 123
  • 154
1

Solution on pure Java using arrays:

public String repeat(char ch, int count) {
    char[] chars = new char[count];
    Arrays.fill(chars, ch);
    return new String(chars);
}
user and
  • 550
  • 4
  • 9