简体   繁体   中英

String padding based on length in Java

So, I have two Strings (line and objectCode)

I want to print line first, and then print a number of spaces based on the length of line and then print objectCode. (So that all of the objectCodes are lined up.

I tried, but got output like:

0000    FIRST   ST      RETADR,LX    172028
0003            LD      BX,#LENGTH    692028
0006    CLOOP   +JSUB   RDREC    03100000

objectCode being the last numbers in each line (172028), as you can see they are not lined up like I want them to be.

So, in essence I want something like:

0000    FIRST   ST      RETADR,LX    172028
0003            LD      BX,#LENGTH   692028
0006    CLOOP   +JSUB   RDREC        03100000

I just can't seem to figure out how to get it. Thank you.

edit

What I have tried:

First try (this is what should have worked):

String write = String.format("%-45s%s", line, objectCode);
fw.write(write + "\n"); //Using a FileWriter

Second I tried (as a last ditch effort):

fw.write(line);
int numOfSpaces = 40 - line.length(); //arbitrary number to check if this works
for (int spaces = 0; spaces < numOfSpaces; spaces++) {
    fw.write(" ");
}
fw.write(objectCode);

I figured it would print less spaces for longer line lengths.. But it didn't seem to work.

EDIT

I have figured out the problem but I don't know how to solve it.

The problem is that earlier in the program I trimmed each line variable (trimming off the preceding and ending white spaces) so I could get each word in the line by itself.

So, I had:

line = input.nextLine();
words[] = line.trim().split("\\s+"); //Splitting by white space

I think the trim() method is my problem here... However, I need it in order to do what the program is intended to do.

Alright so I figured it out and wanted to post the answer in case someone else runs into this problem.

So, like I said the problem was that I was trimming the line variable based on whitespace (in order to get each word in the line separate) :

String line = input.next
String[] words = line.trim().split("\\s+"); //Get each word by itself

The problem was that later in the program I wanted to print something after this line. And there was no way to get the proper length of the line, because all of the whitespaces were gone:

0000    FIRST     ST        RETADR,LX  //line.length = 20 (no whitespace)
0003              LD        BX,#LENGTH //line.length = 16 (no whitespace)

Both lines don't take into account any whitespace in their length. So when I write something after the line like this (using a filewriter)

String write = String.format("%-30s, %s", line, objectCode);
fw.write(write + "\n");

I would get :

0000    FIRST     ST        RETADR,LX       172028
0003              LD        BX,#LENGTH            692028

This is because the spaces aren't accounted for when I allocated the 30 spaces for the line (%30s).

To fix this problem I had to instead of formatting the line itself I would format each word in the line:

String write = String.format("%-10s%-10s%-10s%-10s%", word1, word2, word3, word4);
fw.write(write + "\t" + objectCode + "\n");

So now this effectively gave me what I wanted, because each line is allocated the same amount of space, and they are left aligned (-10s%,etc..)

0000    FIRST     ST        RETADR,LX   172028
0003              LD        BX,#LENGTH  692028

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM