Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Printwriter输出不接受。是否将printf格式化程序转换为文件?_Java_Printwriter - Fatal编程技术网

Java Printwriter输出不接受。是否将printf格式化程序转换为文件?

Java Printwriter输出不接受。是否将printf格式化程序转换为文件?,java,printwriter,Java,Printwriter,使用以下代码输出到已建立的文件时: 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);

使用以下代码输出到已建立的文件时:

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:位于的格式说明符“%.2f” 位于的java.util.Formatter.format(未知源) java.io.PrintWriter.format(未知源代码)位于 java.io.PrintWriter.printf(未知源代码)位于 main(BarakProductOrder.java:45)位于 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于 位于的sun.reflect.NativeMethodAccessorImpl.invoke(未知源) sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于 java.lang.reflect.Method.invoke(未知源代码)位于 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

啊,可怕的逗号做同样的格式化…我想我需要多加注意。是的,解决了,谢谢。Qty是一个int,为什么我需要它呢?@FaridB你不需要它,但是因为你已经在格式化了,在我看来,不需要字符串连接就更容易阅读。