4 java
我试图从println转换为printf,这就是我得到的.
//*********************************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();
}
}
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
更新:解决方案:数字格式将指定的数字格式化为字符串,因为我有%f(浮点数),很明显我会得到一个堆栈,因为你不能将字符串引用到浮点数.感谢HydroKirby在MIrC上的表现.
在
file.printf("Your payment method is %10f\n", payFileOutput);
Run Code Online (Sandbox Code Playgroud)
类型payFileOutput是String,而%10f格式说明符期望消耗浮点数,因此您的提示错误:
java.util.IllegalFormatConversionException: f != java.lang.String
Run Code Online (Sandbox Code Playgroud)
该特定行会导致此错误,但format()如果该方法的返回类型也是String,则调用也可能产生类似的不匹配.