If you can use external libraries, StringUtils.repeat sounds perfect for you:
int s = 5;
System.out.println(StringUtils.repeat('_', s));
EDIT:
To answer the question in the comments - StringUtils.repeat
takes two parameters - the char
you want to repeat and the number of times you want it, and returns a String
composed of that repetition. So, in the example above, it will return a string of five underscores, _____
.