Java:如何编辑和调整文本文件中的变量?

时间:2015-08-04 18:40:12

标签: java

我有一个名为input.txt的文本文件。我想调整该文本文件中的价格[成本]变量,将价格降低一定百分比,然后输出。到目前为止我编写的代码不起作用,所以我甚至没有尝试输出它。我该怎么做才能解决它?

注意:我想尽可能使用if结构。

import javax.swing.JOptionPane;

   import java.io.*;

   public class 111111 
   {
   public static String[] code = new String[1000];
   public static String[] desc = new String[1000];
   public static double[] price = new double[1000];
   public static double[] cost = new double[1000];
   public static double[] inv = new double[1000];
   public static double[] retailValue = new double[1000];
   public static double[] profitValue = new double[1000];

   public static int count = 0;
   public static int totalInv = 0;
   public static double totalRetail = 0.0;
   public static double totalProfit = 0.0;
   public static String[] adjustPrices1;

public static void main(String[] args) throws IOException
{   
    String fileName = "input.txt";
    FileReader inputFile = new FileReader(fileName);
    BufferedReader inputBuffer = new BufferedReader(inputFile); 

    String numString = inputBuffer.readLine();

    while (numString != null)
        if (price[count] < 0) {
            JOptionPane.showMessageDialog(null, "Price cannot be negative, 
    please correct the mistake and restart the program.");
        }
        else if (price[count] < 10.00) 
            price[count] = price[count] * 0.95;

        else if (price[count] >= 10.00 && price[count] <= 99.99)
            price[count] = price[count] * 0.90;

        else if (price[count] >= 100.00 && price[count] <= 499.99)
            price[count] = price[count] * 0.80;

    else 
        price[count] = price[count] * 0.70;

    //Header
        String companyName = inputBuffer.readLine();
        System.out.println(companyName);
        System.out.printf("%-10s %-20s %10s %10s %10s %15s %15s \n", "Code", 
    "Description", "Price", "Cost", "Qty", "Retail Value", "Profit Value");

    //Body
        String numString = inputBuffer.readLine();
        while (numString != null)
        {
            String[] a = numString.split(",");
            code[count] = (a[0]).trim();
            desc[count] = (a[1]).trim();
            price[count] = Double.parseDouble((a[2]).trim());
            cost[count] = Double.parseDouble((a[3]).trim());
            inv[count] = Double.parseDouble((a[4]).trim());
            retailValue[count] = processInput(price[count], inv[count]);
            profitValue[count] = processInput(price[count], cost[count], 
    inv[count]);

            totalInv += inv[count];
            totalRetail += retailValue[count];
            totalProfit += profitValue[count];
            doOutput();
            count ++;
            numString = inputBuffer.readLine();
        }

            //Footer

            System.out.println("------------------------------------------------
    -------------------------------------------------");
            System.out.println("The total number of products read in was " + 
    count + ".");
            System.out.println("The total of inventory items read in were " + 
    totalInv + ".");
            System.out.printf("The total retail value of inventory is $%,.2f. 
    \n", totalRetail);
            System.out.printf("The total profit value of inventory is $%,.2f.     
    \n", totalProfit);
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码存在的问题是您如何阅读文件。您需要逐行读取文件,然后将变量设置为该行上的数字。

take bat