0

I have to make a program where I calculate things and then print it all in one line. But this output also includes max. 5 stars (*****; stars are supposed to be outlined right). So I give my inputs for the calculation, the amount of stars I want and this is for example supposed to be the result:

(50, ***, 60, 20)

for example int a = 5 String s = "*"

Can I do

a * s as result (*****)

Am I supposed to use a for loop here, is there a way to multiply an integer with a string and print it on one line?

korkick
  • 9
  • 1
  • 2
    https://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string Is this what you're looking for? – kmindspark Jan 11 '21 at 22:17

1 Answers1

2

Some options:


If you need up to only 5 stars, substring can be used:

"*****".substring(0, a)

(returns a sub-string beginning at index 0 up to index a-1, that is, of length a). Only works if there is a given/known (small) maximum number of characters.


probably not faster than previous, but easier to understand (and works for more complicated string patterns); use repeat1:

"*".repeat(5)

which repeats (multiplies) the given string a number of times.

1: available since Java 11