Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Add varying whitespace to end of string  RSS feed

 
Frank Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a program that pulls strings from a text file, and outputs the strings and a number. Ex. output:

bat 4
shirt 7
iamabigidiotatjava 8

My problem is I need the numbers to line up. (You don't see it here, but the first two numbers line up with the last line out further). My print statement is:

System.out.println (label + "(tab)" + number); //I don't actually have the word tab in the quotes....it's quote, I hit the tab key, then close quote. FYI, this print statement is in a while loop so it cycles through each line of the text file.

Somehow the longer label causes the tab to move over further. I tried to solve this by breaking it into two print statements:

System.out.print (label);
System.out.println("(22 spaces)" + number); //should print on the same line as label but starting 22 spaces out from the left margin

No dice. The 22 spaces are just added after the label string, so the last line is still off. No label is longer than 18 characters.

I figure I need some way to test the length of the string and then, depending on the length, add a certain number of whitespace characters. Any suggestions?

MY POSSIBLE SOLUTION: (can't test it now). Could I solve this with another while loop, adding spaces directly to the label string? Ex.

labellength = label.length();
while (labellength <18) {
label = label + " ";

}
then do the print statement
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Smith wrote:I figure I need some way to test the length of the string and then, depending on the length, add a certain number of whitespace characters. Any suggestions?


Yes. That's exactly what you should do. Which parts of it are you having trouble with?
 
Frank Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

Thanks for responding. I put my attempt at a possible solution in the original post.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you are going to have to know the length of the longest item before you can put in the number. and you may have to know the length of the longest number, too, depending on what you are going for.

for example, do you want
or
The latter requires you to add extra spaces to the first two lines, since the third line's number is two digits...
 
Frank Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred,

I'd want the second one. No label will be longer than 18 characters. I put my attempt at a solution in the original post.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use "\t" for embedding tabs in strings, btw.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using Java 1.5 or later, you can use the String.format method
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!