Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: printf stack error

Tags:

java

I was trying to convert from println to printf and this is what I got.

//*********************************Output File*********************************
//Create a file with the information entered 
//and the information processed
 public void outputFile() throws IOException{
String payFileOutput=null;
PrintWriter file= new PrintWriter("DataOutput.txt");
file.printf("Your total expenses per month are %10f\n", 
   format.format(getTotalCost()));
file.printf("Your college tuition is %10f\n", format.format(getTuition()));
file.printf("Your rent is %10f\n", format.format(getRent()));
  if(pay==1)
   payFileOutput="Savings";
  else if(pay==2)
   payFileOutput="Loans";
  else if(pay==3)
   payFileOutput="Freelance Work";
  else
   ;
  file.printf("Your payment method is %10f\n", payFileOutput);
  file.printf("Your amount entered for the payment method is %10f\n", 
     format.format(getPayment()));
  if(totalCost<0){
   file.printf("You still need: %5f per month\n", 
     format.format(getTotalCost()));}
  else{
   file.printf("\nYour budget seems good");}
  file.close();
 }
}  

Exception in thread "AWT-EventQueue-0" java.util.IllegalFormatConversionException: f != java.lang.String
 at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4011)
 at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2738)
 at java.util.Formatter$FormatSpecifier.print(Formatter.java:2683)
 at java.util.Formatter.format(Formatter.java:2449)
 at java.io.PrintWriter.format(PrintWriter.java:878)
 at java.io.PrintWriter.printf(PrintWriter.java:777)
 at FinanceRev1.outputFile(FinanceRev1.java:173)
 at FinanceGUI$button2Listener.actionPerformed(FinanceGUI.java:286)
 at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
 at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
 at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
 at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
 at java.awt.Component.processMouseEvent(Component.java:6175)
 at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
 at java.awt.Component.processEvent(Component.java:5940)
 at java.awt.Container.processEvent(Container.java:2105)
 at java.awt.Component.dispatchEventImpl(Component.java:4536)
 at java.awt.Container.dispatchEventImpl(Container.java:2163)
 at java.awt.Component.dispatchEvent(Component.java:4362)
 at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
 at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
 at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
 at java.awt.Container.dispatchEventImpl(Container.java:2149)
 at java.awt.Window.dispatchEventImpl(Window.java:2478)
 at java.awt.Component.dispatchEvent(Component.java:4362)
 at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
 at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
 at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

Update: the solution: Number format formatted the indicated number to a String, and because I had %f (float) it was pretty obvious I would get a stack since you cant reference a string to a float. Thanks to HydroKirby on MIrC.


1 Answers

In

file.printf("Your payment method is %10f\n", payFileOutput);

the type of payFileOutput is String, whereas the %10f format specifier is expecting to consume a float, hence your tip error:

java.util.IllegalFormatConversionException: f != java.lang.String

That particular line is one that would give rise to this error, but your calls to format() may also produce similar mismatches if the return type of that method were also String.

like image 188
martin clayton Avatar answered May 05 '26 21:05

martin clayton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!