Java error: printf

I tried 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 : solution: the format of the number formatted the specified number to a string, and since I had% f (float), it was pretty obvious that I was getting a stack since you cannot reference the string float. Thanks to HydroKirby at MIrC.

+4
source share
1 answer

IN

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

the payFileOutput type is String, while the %10f format specifier expects float consumption, so your hint error:

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

This particular line is the one that would generate this error, but your calls to format() can also lead to similar inconsistencies if the return type of this method was also a line.

+9
source

Source: https://habr.com/ru/post/1332281/


All Articles