views:

271

answers:

3

I am reading the serial buffer with readLine() method. The string returned by readLine() is in the format "str1 : str2". In a while loop when I use readLine() number of times for a response of serial port command I get weird output like this :

String1 : string1
SString11 : String2
StringString2 : String23 
String4 : String5

But I need the output formatted as below

String1       : string1
SString11     : String2
StringString2 : String23 
String4       : String5

I used the split method on string and get the two strings separated with delimiter as ':'. But now I need to append the String1 with spaces to align all the colons.

I am sorry If my problem explanation is weird. But If anybody understood the problem can you please suggest how to do it?

+3  A: 

Take a look at System.out.printf

TofuBeer
I used String.format() and It worked. But another problem is there. Problem is I am putting the output string in a JTextArea instance. I have choosen the font as Arial. When print like this String.format("%-30s:%30s",temp[0],temp[1]);It prints likeImage Name : Basic Linux firmware Version : 2.1.1.17 In the above two lines "I" in word Image and "V" in word Version has different width on text area. So even though i m using 30 character string the colons are not aligning. Is there any solution for this?
Surjya Narayana Padhi
take a look at java.awt.FontMetrics (maybe... been a long time since I cared about the width of a text field/area :-)
TofuBeer
You need to use a font such as Courier, this is referred to as a 'monospaced' font meaning that all the characters have equal width. This would of course mean that if you characters align in the string the would align on ouput as well.
Peter Lindqvist
+7  A: 

The easiest way to achieve this is to use System.out.format(). This lets you use C printf style format specifiers. An example:

System.out.format("%-5s:%10s\n", "Name", "Nemo");
System.out.format("%-5s:%10d\n", "Age", 120);

This will output:

Name :      Nemo
Age  :       120

Also see documentation for the Formatter class to learn more about the format string syntax.

Vijay Mathew
Thanks for the answer. Can I get the formated output stored in a string instead of sending to System.out stream? Is there any API?
Surjya Narayana Padhi
You can use String.format() the same way as System.out.format() to get the result as a String.
tangens
+1 for linking to java.util.Formatter which is possibly more useful for the question poster than System.out's format() or printf() as it can format to arbitrary streams and it's not clear in the post what the destination of the output is.
charstar
@Surjya Narayana Padhi To get the formatted output stored in a string, use String.format().
Vijay Mathew
I used String.format() and It worked. But another problem is there. Problem is I am putting the output string in a JTextArea instance. I have choosen the font as Arial. When print like this String.format("%-30s:%30s",temp[0],temp[1]); It prints like Image Name : Basic Linux firmware Version : 2.1.1.17 In the above two lines "I" in word Image and "V" in word Version has different width on text area. So even though i m using 30 character string the colons are not aligning. Is there any solution for this?
Surjya Narayana Padhi
Used a fixed width font like Courier New, Lucida Console, Bitstream Vera Sans Mono, Consolas, etc
basszero
A: 

System.out.format("%30s : %s", str1, str2);

Will print the first string with a fixed width of 30 characters.

Mayuresh