[英]Java: How can I edit and adjust a variable in a text file?
我有一个名为input.txt的文本文件。 我想调整该文本文件中的price [cost]变量,以将价格降低一定百分比,然后输出。 到目前为止,我已编译的代码无法正常工作,因此我什至没有尝试输出它。 我该如何解决?
注意:我想尽可能使用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);
}
}
代码的问题是如何读取文件。 您需要逐行读取文件,然后将变量设置为该行上的数字。
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
price[count] = Double.valueOf(line);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.