2

During research for a task (create a receipt) I found the following code:

public class Kassenzettel {
    public static void main(String[] args) {

        // Einzelpreise - Waren
        double SEBox = 299.00;
        double Hauptplatine = 79.80;
        double Erweiterungsplatine =52.60;
        double Anschlusskabel = 19.50;
        double Firmware = 36.00;

        // Anzahl - Waren
        int anzSEBox = 1;
        int anzHauptplatine = 1;
        int anzErweiterungsplatine = 1;
        int anzAnschlusskabel = 2;
        int anzFirmware = 2;

        // Inhalt - Brieftasche
        double brieftasche = 550.00;

        // Gekaufte Waren
        double summe = 0;
        summe = summe + anzSEBox * SEBox;
        summe = summe + anzHauptplatine * Hauptplatine;
        summe = summe + anzErweiterungsplatine * Erweiterungsplatine;
        summe = summe + anzAnschlusskabel * Anschlusskabel;
        summe = summe + anzFirmware * Firmware;

        if (summe > brieftasche) {
            System.out.println("Nicht genuegend Geld in der Brieftasche");
        } else {

            System.out.println("____________________________________");
            System.out.println("DATCOM protelematik GmbH - Kassenbon");
            System.out.println("____________________________________");

            System.out.println(String.format("%-9s %2d x %5.2f EUR", "SEBox", anzSEBox, SEBox));
            System.out.println(String.format("%30.2f EUR", anzSEBox * SEBox));

            System.out.println(String.format("%-9s %2d x %5.2f EUR", "Hauptplatine", anzHauptplatine, Hauptplatine));
            System.out.println(String.format("%30.2f EUR", anzHauptplatine * Hauptplatine));

            System.out.println(String.format("%-9s %2d x %5.2f EUR", "Erweiterungsplatine", anzErweiterungsplatine, Erweiterungsplatine));
            System.out.println(String.format("%30.2f EUR", anzErweiterungsplatine * Erweiterungsplatine));

            System.out.println(String.format("%-9s %2d x %5.2f EUR", "Anschlusskabel", anzAnschlusskabel, Anschlusskabel));
            System.out.println(String.format("%30.2f EUR", anzAnschlusskabel * Anschlusskabel));

            System.out.println(String.format("%-9s %2d x %5.2f EUR", "Firmware", anzFirmware, Firmware));
            System.out.println(String.format("%30.2f EUR", anzFirmware * Firmware));

            System.out.println("____________________________________");

            System.out.println(String.format("%-9s %20.2f EUR", "Gesamt", summe));
            System.out.println(String.format("%-9s %20.2f EUR", "Gegeben", brieftasche));
            System.out.println();
            System.out.println(String.format("%-9s %20.2f EUR", "Zurueck", brieftasche - summe));
        }
    }
}

But I don't understand the following line:

System.out.println(String.format("%-9s %2d x %5.2f EUR", "Anschlusskabel", anzAnschlusskabel, Anschlusskabel));

I do understand what System.out.println is for, but can someone explain to me what exactly this is for: "%-9s %2d x %5.2f EUR"?

I think it's for the part where the code says:

SEBox      1 x 299,00 EUR
                        299,00 EUR
Hauptplatine  1 x 79,80 EUR
                         79,80 EUR
Erweiterungsplatine  1 x 52,60 EUR
                         52,60 EUR
Anschlusskabel  2 x 19,50 EUR
                         39,00 EUR
Firmware   2 x 36,00 EUR
                         72,00 EUR

but why and how?

Tom
  • 16,842
  • 17
  • 45
  • 54
Laura
  • 19
  • 4

1 Answers1

6

String.format is used to format the string.

% is a special character denoting that a formatting instruction follows.

Type of argument is denoted by d, f, x and s.

  • integers - d,

  • strings - s,

  • floating point - f

  • integers with hex format- x.

%-9s : will format the string as it is. If the string has less than 9 characters, the output will be padded on the right.

%2d : will format the integer as it is. If the number of digits is less than 2, the output will be padded on the left.

%5.2f : will format maximum 2 decimal digits of the number. The output will occupy 5 characters at least. If the number of digits is not enough, it will be padded.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
aru007
  • 370
  • 3
  • 11