gpt4 book ai didi

Java:printf 堆栈错误

转载 作者:搜寻专家 更新时间:2023-11-01 01:38:31 24 4
gpt4 key购买 nike

我试图将 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();
}
}

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)

更新:解决方案:数字格式将指示的数字格式化为字符串,并且因为我有 %f( float ),所以很明显我会得到一个堆栈,因为您不能将字符串引用到一个花车。感谢 MIrC 上的 HydroKirby。

最佳答案

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

payFileOutput 的类型是 String,而 %10f 格式说明符期望使用 float ,因此您的提示错误:

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

该特定行会导致此错误,但如果该方法的返回类型也是字符串,您对 format() 的调用也可能产生类似的不匹配。

关于Java:printf 堆栈错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4455951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com