[英]program that uses input file and creates a new one
我正在編寫一個代碼,該代碼在應創建的程序中使用一個名為InvetoryReport.txt的輸入文件,該文件應采用此文件,然后將文件中的兩個數據相乘,然后使用此數據創建一個新文件。 同樣在程序的開頭,應該詢問您輸入文件的名稱。 您有3次機會,那就是通知您它找不到它,現在將退出,然后停止執行。
我的輸入文件是這個
Bill 40.95 10
Hammer 1.99 6
Screw 2.88 2
Milk .03 988
(該程序應該將列中的兩個數字相乘,然后用總和創建一個新列,然后在該行下打印另一行,例如“
Bill 40.95 10 409.5
Hammer 1.99 6 11.94
Screw 2.88 2 5.76
Milk .03 988 29.64
Total INVENTORY value $ 456.84"
我到目前為止的程序是
package textfiles;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.IOException;
public class LookOut{
double total = 0.0;
String getFileName(){
System.out.printIn("Type in file name here.");
try {
int count =1;
FileReader fr = new FileReader("InventoryReport.txt");
BufferedReader br = new BufferedReader(fr);
String str;
while ((str = br.readLine()) != null) {
out.println(str + "\n");
}
br.close();
} catch (IOException e) {
if(count == 3) {
System.out.printIn("The program will now stop executing.");
System.exit(0);
count++;
}
}
return str;
}
void updateTotal(double d){
total = total + d;
}
double getLineNumber(int String_line){
String [] invRep = line.split(" ");
Double x = double.parseDouble(invRep[1]);
Double y = double.parseDouble(invRep[2]);
return x * y;
}
void printNewData(String = newData) {
PrintWriter pW = new PrintWriter ("newData");
pw.print(newData);
pw.close;
}
public static void main(String[] args){
String str = ("Get file name");
String str = NewData("InventoryReport/n");
File file = new File(str);
Scanner s = new Scanner(file);
while(s.hasNextLine()) {
String line = s.nextLine();
double data = getLineNumber(line);
update total(data);
NewData += line + " " + data + "/n";
Print NewData(NewData);
}
}
}
我收到似乎無法弄清的多個錯誤代碼。
try {
int count =1;
FileReader fr = new FileReader("InventoryReport.txt");
BufferedReader br = new BufferedReader(fr);
String str;
while ((str = br.readLine()) != null) {
br.close();
} catch (IOException e) {
if(count == 3) {
System.out.printIn("The program will now stop executing.");
System.exit(0);
count++;
}
}
盡管有您的最佳意圖,但實際上您實際上缺少一個'}'。 請注意,在捕獲之前您沒有逃過Try塊。 我想這是因為您將while語句的close}與try塊的close}混淆了。 改為這樣做:
try {
int count =1;
FileReader fr = new FileReader("InventoryReport.txt");
BufferedReader br = new BufferedReader(fr);
String str;
while ((str = br.readLine()) != null) {
br.close();
}
}
catch (IOException e) {
if(count == 3) {
System.out.printIn("The program will now stop executing.");
System.exit(0);
count++;
}
}
另外,您的縮進范圍很廣。 這應該給您一個教訓,說明為什么要正確格式化代碼! 如果格式不正確,那么很容易錯過簡單的語法錯誤。 其他人也很難閱讀您的代碼並弄清楚它出了什么問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.