Java String Variable in printf ausgeben
hallo,
wie kriege ich in einem Einzeiler ohne "+" die Ausgabe von String-Variablen hin?
danke.
wie kriege ich in einem Einzeiler ohne "+" die Ausgabe von String-Variablen hin?
import java.text.*;
public class StundenlohnFix {
public static void main(String args) {
DecimalFormat ZahlFormatieren = new DecimalFormat("#0.00");
int Stunden = 40;
double Gehalth = 20.00;
String Waehrung ="Euro";
System.out.printf("Arbeitsstunden: %d \n", Stunden);
System.out.printf(String.format("Stundenlohn: %s %S", ZahlFormatieren.format(Gehalth)) , Waehrung); // Hier ist der Fehler
//System.out.println(ZahlFormatieren.format(Gehalth));
System.out.print("Damit habe ich letzte Woche ");
System.out.print(ZahlFormatieren.format(Stunden * (Gehalth)));
System.out.println(" Euro verdient.");
}
}
Arbeitsstunden: 40
Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier 'S'
at java.util.Formatter.format(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.lang.String.format(Unknown Source)
at StundenlohnFix.main(StundenlohnFix.java:14)
danke.
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 274422
Url: https://administrator.de/forum/java-string-variable-in-printf-ausgeben-274422.html
Ausgedruckt am: 02.04.2025 um 05:04 Uhr
1 Kommentar

Da reicht ein :
oder nach deiner Variante:
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
Gruß jodel32
System.out.format("Stundenlohn: %.2f %s", Gehalth , Waehrung);
System.out.format("Stundenlohn: %s %s", ZahlFormatieren.format(Gehalth) , Waehrung);
Gruß jodel32