marabunta
Goto Top

Java String Variable in printf ausgeben

hallo,

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.

Content-Key: 274422

Url: https://administrator.de/contentid/274422

Printed on: April 24, 2024 at 09:04 o'clock

Mitglied: 114757
Solution 114757 Jun 12, 2015 updated at 13:00:52 (UTC)
Goto Top
Da reicht ein :
System.out.format("Stundenlohn: %.2f %s", Gehalth , Waehrung);  
oder nach deiner Variante:
System.out.format("Stundenlohn: %s %s", ZahlFormatieren.format(Gehalth) , Waehrung);  
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

Gruß jodel32