1

I'm trying to format Strings as they arrive and meet certain criteria. However if the tokens below are of different length things don't match up, How do I do this? I want all the "IP address"s to match up. I assume I use something like %20s?

if (tokens.length == 4 && (tokens[3].equals("up") || tokens[3].equals("down"))){
    String result = String.format("%s is %s. IP address: %s \n", tokens[0], tokens[2], tokens[1]);
    final String ReceivedText = mReceiveBox.getText().toString() + result;

    if(tokens[2].equals("up")){
        runOnUiThread(new Runnable() {
        public void run() {
            mReceiveBox.setText(ReceivedText);
            mReceiveBox.setSelection(ReceivedText.length());

         }
      });
   }
}
3
  • 1
    Could you show an example of the output not lining up? Commented Mar 6, 2013 at 14:46
  • Here you go, on the right. i.imgur.com/eBlr2Yt.png The bottom IP starts too soon, I'd like to make more space between the previous line and IP for each line, and the bottom one should line up with the previous lines Commented Mar 6, 2013 at 14:53
  • So maybe I should change everything to a %s. Commented Mar 6, 2013 at 14:56

2 Answers 2

3

Here's an example of right padding a String with spaces:

System.out.println(String.format("%-40s", "SHORT TEXT") + "|");
System.out.println(String.format("%-40s", "SOME LONG TEXT") + "|");
System.out.println(String.format("%-40s", "SOME MUCH LONGER TEXT") + "|");

Which outputs:

SHORT TEXT                              |
SOME LONG TEXT                          |
SOME MUCH LONGER TEXT                   |

The - formatting flag will left justify your text. Without it, the output looks like:

                              SHORT TEXT|
                          SOME LONG TEXT|
                   SOME MUCH LONGER TEXT|

Also, as mentioned in another answer, you'll have to use a monospaced font if you want everything to align correctly.

Here are 2 lines of 10 characters each in a font which is not monospaced:

wwwwwwwwww
iiiiiiiiii

And those same 2 lines in a monospaced font:

wwwwwwwwww
iiiiiiiiii
Sign up to request clarification or add additional context in comments.

1 Comment

Ill try this out, seems similar to what I was doing so i dont know why its not working. I have a monospace font now thanks! android:typeface="monospace"
1

Look into specifying a width in your format string, and include mReceiveBox.getText().toString() in your format (instead of appending result to it).

See: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax

You want to end up with something like:

String result = String.format("%20s%s is %s. IP address: %s \n",
    mReceiveBox.getText().toString(), tokens[0], tokens[2], tokens[1]);

Where 20 is the maximum width for the receive box text. You'll need to do the same for the tokens preceding the IP as well.

EDIT: Also, from looking at your image, you're doing to need to used a fixed width font in your output. Otherwise you're going to need to start computing widths of rendered text in pixels. A much trickier problem.

EDIT 2: The font matters because if it's not fixed width, the width will actually depend on the content of your string. Consider the following (assuming you're using a standard font on a standard browser):

lllll that was five characters long
mmmmm that was five characters long

vs.

lllll that was five characters long
mmmmm that was five characters long

See the alignment issues?

4 Comments

Ive tried things like String result = String.format("%10s %2s %2s%s %2s %8s %s \n", tokens[0], "is", tokens[2], ".", "IP", "address:", tokens[1]); and while it is better, it's not the best still. I'm not sure why the font width is an issue? I know that things are different widths atm, but when I was trying \t it lines everything up anyway, so I assumed some string format method would do that too, maybe I just thought they were lined up
OK, good. I had just edited the answer to illustrate the font issue.
Done now but the only problame is it looks bad as I am allowing for the maximum length that the first token can be. eg i.imgur.com/q7YLgmC.png vs i.imgur.com/RtnoSpT.png
Right. So you could iterate over all of them first, compute the maximum length in your data set, and then dynamically set your width. In other words, you would need to compose your format string dynamically before applying the format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.