Question

I have a string "name" which I need to add n amount of blank spaces to the end. I have been told to use String.format but for the life of me I cant figure out how to do it.

String formatName = String.format(name, %15s);
return formatName;

But this didn't work, can anyone point me in the right the direction?

Basically I need to make each string 15 characters long with blank spaces appended to the end if the original string is too short.

================

With advice I reversed the paramaters however this throws up an error.

private String format(String name, String number) 
{
    String formatName = String.format(%15s, name);
    String formatNumber = String.format(%15s, number);
    return formatName + " - " + formatNumber;
}

However this throws an error - Illegal start of expression.

Can anyone tell me what I'm doing wrong?

Was it helpful?

Solution

formatName = String.format(name + "%15s", "");

OR

formatName = String.format("%-15s", name);

The - appends to the end of the argument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top