Printwriter 输出不接受 .printf 格式化程序到文件?
Printwriter output not accepting .printf formatters to file?
使用以下代码输出到已经建立的文件时:
import java.util.Scanner;
import java.io.*;
public class BarakProductOrder
{
public static void main(String[] args) throws IOException
{
//Establish scanner object
Scanner input = new Scanner (System.in);
//Declare variables
String name, product, street, city, state, zip;
int qty;
double price;
//Collect data
System.out.println("Welcome to Matt's Dog Food Emporium!\n\nWhat is your name?");
name = input.nextLine();
System.out.println("Which product would you like to order today?");
product = input.nextLine();
System.out.println("How many would you like?");
qty = input.nextInt();
price = 4.56*qty;
input.nextLine(); //Must be placed to not skip over "address" line
System.out.println("Enter your street address:");
street = input.nextLine();
System.out.println("Enter the City:");
city = input.nextLine();
System.out.println("Enter the State (ex.: NY):");
state = input.nextLine();
System.out.println("Enter the Zip:");
zip = input.nextLine();
System.out.println("Thank you for your order, " + name);
//Output data to file "BarakOrder.txt"
PrintWriter outputFile = new PrintWriter("C:\Users\fbara\Desktop\CISS 110\BarakOrder.txt");
outputFile.println("Order Number: 001");
outputFile.println("Product: " + product);
outputFile.println("Quantity: " + qty);
outputFile.printf("Price = 4.56*" + qty + " = $" + "%.2f" + price);
outputFile.println("Customer Contact Info: " + name);
outputFile.println("Address: " + street);
outputFile.println(city + ", " + state + " " + zip);
//Close scanner and file object
input.close();
outputFile.close();
System.out.println("Data has been written.");
}
}
在第 45 行使用“%.2f”修饰符会出现以下错误消息:
java.util.MissingFormatArgumentException: Format specifier '%.2f' at
java.util.Formatter.format(Unknown Source) at
java.io.PrintWriter.format(Unknown Source) at
java.io.PrintWriter.printf(Unknown Source) at
BarakProductOrder.main(BarakProductOrder.java:45) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at
edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)
我在 outputFile.printf
处使用了错误的修饰符吗?或者错误的“.print()”行?
那不是 printf
的工作方式;你给它一个格式然后打印的东西。
您没有将任何内容传递给 %.2f
格式,您正在尝试添加它。文档提供了有关如何使用它的信息(世界上有很多示例)。
大致:
outputFile.printf("Price = 4.56*" + qty + " = $" + "%.2f", price);
尽管我不确定您为什么不对 qty
进行相同的格式化。
使用以下代码输出到已经建立的文件时:
import java.util.Scanner;
import java.io.*;
public class BarakProductOrder
{
public static void main(String[] args) throws IOException
{
//Establish scanner object
Scanner input = new Scanner (System.in);
//Declare variables
String name, product, street, city, state, zip;
int qty;
double price;
//Collect data
System.out.println("Welcome to Matt's Dog Food Emporium!\n\nWhat is your name?");
name = input.nextLine();
System.out.println("Which product would you like to order today?");
product = input.nextLine();
System.out.println("How many would you like?");
qty = input.nextInt();
price = 4.56*qty;
input.nextLine(); //Must be placed to not skip over "address" line
System.out.println("Enter your street address:");
street = input.nextLine();
System.out.println("Enter the City:");
city = input.nextLine();
System.out.println("Enter the State (ex.: NY):");
state = input.nextLine();
System.out.println("Enter the Zip:");
zip = input.nextLine();
System.out.println("Thank you for your order, " + name);
//Output data to file "BarakOrder.txt"
PrintWriter outputFile = new PrintWriter("C:\Users\fbara\Desktop\CISS 110\BarakOrder.txt");
outputFile.println("Order Number: 001");
outputFile.println("Product: " + product);
outputFile.println("Quantity: " + qty);
outputFile.printf("Price = 4.56*" + qty + " = $" + "%.2f" + price);
outputFile.println("Customer Contact Info: " + name);
outputFile.println("Address: " + street);
outputFile.println(city + ", " + state + " " + zip);
//Close scanner and file object
input.close();
outputFile.close();
System.out.println("Data has been written.");
}
}
在第 45 行使用“%.2f”修饰符会出现以下错误消息:
java.util.MissingFormatArgumentException: Format specifier '%.2f' at java.util.Formatter.format(Unknown Source) at java.io.PrintWriter.format(Unknown Source) at java.io.PrintWriter.printf(Unknown Source) at BarakProductOrder.main(BarakProductOrder.java:45) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)
我在 outputFile.printf
处使用了错误的修饰符吗?或者错误的“.print()”行?
那不是 printf
的工作方式;你给它一个格式然后打印的东西。
您没有将任何内容传递给 %.2f
格式,您正在尝试添加它。文档提供了有关如何使用它的信息(世界上有很多示例)。
大致:
outputFile.printf("Price = 4.56*" + qty + " = $" + "%.2f", price);
尽管我不确定您为什么不对 qty
进行相同的格式化。