Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Trying to align text in collumns

I'm trying to format the text in collums using java.util.Formatter and Ive set the padding using the following

Code
String.format("%s %45s %50s", name, amount, dropRate);

and here's the result, I can't get it to quite align properly though can somebody help me?

[Image: direct]
I'm not familiar with Java's format but...
Code
String.format("%s %45s %50s", name, amount, dropRate);
Shouldn't that be something like...
Code
String.format("Item: %-20s Amount: %-45s Drop rate: %-50s", name, amount, dropRate);
(Feb 7, 2017 04:53 AM)g4143 Wrote:  I'm not familiar with Java's format but...
Code
String.format("%s %45s %50s", name, amount, dropRate);
Shouldn't that be something like...
Code
String.format("Item: %-20s Amount: %-45s Drop rate: %-50s", name, amount, dropRate);

Yes, I've just put the variable names in each individual string along with the value.

Here's the full toString method
Code
public String toString() {
        String name = "@cya@Item: @or2@" + ItemDefinition.getItem(itemId).getName();
        String tempAmount = amount;
        if(tempAmount.toLowerCase().startsWith("random")) {
            String[] data = tempAmount.substring(7).split(":");
            tempAmount = String.format("%s - %s", data[0], data[1]);
        }
        String amount = "@cya@Amount: @or2@" + tempAmount;
        String dropRate = "@cya@Drop rate: @or2@" + numerator + "/" + denominator;
        return String.format("%s %45s %50s", name, amount, dropRate);
    }
(Feb 7, 2017 10:39 AM)matt Wrote:  Yes, I've just put the variable names in each individual string along with the value.

Here's the full toString method
Code
public String toString() {
        String name = "@cya@Item: @or2@" + ItemDefinition.getItem(itemId).getName();
        String tempAmount = amount;
        if(tempAmount.toLowerCase().startsWith("random")) {
            String[] data = tempAmount.substring(7).split(":");
            tempAmount = String.format("%s - %s", data[0], data[1]);
        }
        String amount = "@cya@Amount: @or2@" + tempAmount;
        String dropRate = "@cya@Drop rate: @or2@" + numerator + "/" + denominator;
        return String.format("%s %45s %50s", name, amount, dropRate);
    }

Isn't there an escape character in java "\t" I believe that tabs things over, couldn't you just tab everything according to it's column?

Code
String example = "\tFirst Column \tSecond Column \tThird Column";
[Image: 0d3aOy6.gif]
RS has custom text rendering, tab characters aren't going to work. You're going to want to make 3 labels for each line, and align the x coordinate.
(Feb 8, 2017 02:20 PM)clrlslwi Wrote:  RS has custom text rendering, tab characters aren't going to work. You're going to want to make 3 labels for each line, and align the x coordinate.

This clears some stuff up, thanks man! :)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by -Matatan-Nov 25, 2016 05:15 PM 9 252 Nov 25, 2016 05:15 PM
Last Post: -Matatan-
[.NET] by Exodus.Feb 13, 2016 05:55 AM 10 333 Feb 13, 2016 05:55 AM
Last Post: Ninja
[Java] by ctxdMay 20, 2013 02:37 PM 7 104 May 20, 2013 02:37 PM
Last Post: ctxd