Hack Forums
[Java] Trying to align text in collumns - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Trying to align text in collumns (/showthread.php?tid=5545271)



Trying to align text in collumns - matt - Feb 7, 2017

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]


* - g4143 - Feb 7, 2017

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);



* - matt - Feb 7, 2017

(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);
    }



* - _thenotsofriendlyghost - Feb 8, 2017

(Feb 7, 2017 10:39 AM)Midnight Blue™ 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";



* - clrlslwi - Feb 8, 2017

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.


* - matt - Feb 11, 2017

(Feb 8, 2017 02:20 PM)Demmonic 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! :)