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.