使用Java中的BufferedWriter格式化双输出

时间:2021-05-11 16:26:07

I'm working on an assignment, and have completed the code, however I am having an issue with correct formatting of the output. I am taking a text file, separating the products based by price, and appending the data to two separate files. How do I make the format of the double correct with the BufferedWriter so that it appears as money value (0.00) without the dollar sign? It currently outputs as (0.0). Thanks for your help.

我正在完成一项任务,并完成了代码,但是我遇到了输出格式正确的问题。我正在获取一个文本文件,根据价格分离产品,并将数据附加到两个单独的文件中。如何使用BufferedWriter使双重格式更正,以便它显示为没有美元符号的货币值(0.00)?它目前输出为(0.0)。谢谢你的帮助。

My code:

//import the io utilities and scanner
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.Scanner;


/**
 * class ProductData uses the scanner utility to read a file, FileWriter and
 * BufferedWriter to write data from original file to two new files
 * based on the product price.
 */
public class ProductData
{
    /**
     * method main calls the scanner to read a file, uses a while loop
     * to scan through the file until done, and uses two if loops
     * to determine the price and append the data to two new files.
     * @param args
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException
    {


        //create a scanner, call the file to be read
        Scanner reader = new Scanner(new File("products.txt"));

        //variables defined
        int productID;
        String productDescription;
        double productPrice;


        //while loop to read each line in the file
        while (reader.hasNext())
        {
            //retrieves each item in order using the next feature of the scanner
            productID = reader.nextInt();
            productDescription = reader.next();
            productPrice = reader.nextDouble();

            //if loop for price greater than or equal to 50.00
            if(productPrice >= 50.00)
            {
                try
                {
                    // Create file
                    FileWriter fstream = new FileWriter("WishList.txt",true);
                    //create BufferedWriter to write data to the file
                    BufferedWriter out = new BufferedWriter(fstream);
                    //writes data and blank line to file
                    out.write(productID + " " + productDescription + " " + productPrice );
                    out.newLine();
                    //Close the output stream
                    out.close();
                }

                catch (Exception e)
                {
                    //Catch exception if any
                    System.err.println("Error: " + e.getMessage());
                }

            }

            //if loop for price less than 50.00
            if(productPrice < 50.00)
            {
                try
                {
                    // Create file
                    FileWriter fstream = new FileWriter("GiftIdeas.txt",true);
                    //creates BufferedWriter to write data to file
                    BufferedWriter out = new BufferedWriter(fstream);
                    //writes data and blank line to file
                    out.write(productID + " " + productDescription + " " + productPrice );
                    out.newLine();
                    //Close the output stream
                    out.close();
                }

                catch (Exception e)
                {
                    //Catch exception if any
                    System.err.println("Error: " + e.getMessage());
                }

            }//ends if loop
        }//ends while loop
    }//ends method main
}

3 个解决方案

#1


For Example:

out.write(productID + " " + productDescription + " " + 
String.format("%.2f",productPrice) );

#2


Use String.format(pattern, args). This lets you easily specify formatting rules for items such as doubles. It's a fairly complex method, so read up on the javadocs to see how it works.

使用String.format(pattern,args)。这使您可以轻松指定双打等项目的格式规则。这是一个相当复杂的方法,所以请阅读javadoc以了解它是如何工作的。

#3


Have a look at DecimalFormat.

看看DecimalFormat。

#1


For Example:

out.write(productID + " " + productDescription + " " + 
String.format("%.2f",productPrice) );

#2


Use String.format(pattern, args). This lets you easily specify formatting rules for items such as doubles. It's a fairly complex method, so read up on the javadocs to see how it works.

使用String.format(pattern,args)。这使您可以轻松指定双打等项目的格式规则。这是一个相当复杂的方法,所以请阅读javadoc以了解它是如何工作的。

#3


Have a look at DecimalFormat.

看看DecimalFormat。