#How can I format the message for JOptionPane dynamically?

9 messages · Page 1 of 1 (latest)

exotic tendon
#
      public static Comic[] createComics() {
        Comic[] comics = new Comic[] {
                new Comic("978-0785199618", "Spider-Man:Miles Morales", 112, 0.72),
                new Comic("978-0785190219", "Ms.Marvel:No Normal", 120, 0.80),
                new Comic("978-0785198956", "Secret Wars", 312, 1.75),
                new Comic("978-0785156598", "Infinity Gauntlet", 256, 1.42)
        };
        return comics;
    }
    

I am fairly new to java and right now I am at a roadblock for displaying that message using joptionpane. I don't want to hardcode it exactly but I have zero clue on how to format my message to the one similar to the image. I'm assuming I have to use String.format but I couldn't find any resources that helps me. The code above is the method for the fake database of comics.

slender knotBOT
#

Hey, @exotic tendon!
Please remember to /close this post once your question has been answered!

exotic tendon
#

String.format("%-25s | %-35s | %-6s | %-1s | %s %n------------------------------------------------------------------------------------------------", "ISBN-13", "Title", "Pages", "Price/Day", "Deposit"); This is all I could come up with thus far

hybrid remnant
#

./run java

var fmt = "%-25s | %-35s | %-6s | %-1s | %s%n";
var sb= new StringBuilder();
sb.append(String.format(fmt, "ISBN-13", "Title", "Pages", "Price/Day", "Deposit"));
sb.append("------------------------------------------------------------------------------------------------\n");
for (int i= 0; i< 5; i++) {
    sb.append(String.format(fmt, "isbn " + i, "title"+i, "pages"+i, "price day"+i, "deposit"+i));
}
System.out.println(sb.toString());
sinful ventureBOT
#

Here is your java(15.0.2) output @hybrid remnant

ISBN-13                   | Title                               | Pages  | Price/Day | Deposit
------------------------------------------------------------------------------------------------
isbn 0                    | title0                              | pages0 | price day0 | deposit0
isbn 1                    | title1                              | pages1 | price day1 | deposit1
isbn 2                    | title2                              | pages2 | price day2 | deposit2
isbn 3                    | title3                              | pages3 | price day3 | deposit3
isbn 4                    | title4                              | pages4 | price day4 | deposit4

hybrid remnant
#

Something like this? 👆

exotic tendon
#

yup! thank you so much