我需要創建一個接受來自檔案的輸入的程式。我需要讓用戶輸入兩(2)個變數,即貸款利率和月份。然后我計算每月付款,從檔案中輸出原始輸入以及貸款月數和每月付款。
我在將陣列中的數字轉換為 int 時遇到問題,以便我可以計算它們。我已經嘗試了一些事情,但不能讓它做我想做的事。在閱讀了其他一些問題后,我能夠找到如何將陣列轉換為 int 并獲得總和,因此我將其包含在代碼中。在將“專案”陣列轉換為 int 后,我??知道如何進行計算。我只是在將 item[1] 轉換為可用于計算專案總和的陣列方面尋求幫助。我在代碼中包含了可能更好地顯示我正在尋找的內容的注釋。
這是輸入檔案的樣子:
Driver 425
Putter 200
Wedges 450
Hybrid 175
這是我的代碼:
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.*;
public class Assignment3t {
public static void main(String[] args) {
File inputFile = new File("Project3.txt");
File outputFile = new File("Project3Output.txt");
Scanner scanner = new Scanner(System.in);
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
System.out.print("Enter the interest rate: ");
float interestRate = scanner.nextFloat();
System.out.print("Enter months for the loan: ");
int loanMonths = scanner.nextInt();
try {
bufferedReader = new BufferedReader(new FileReader(inputFile));
bufferedWriter = new BufferedWriter(new FileWriter(outputFile));
String line;
while ((line = bufferedReader.readLine()) !=null) {
String[] item = line.split("\\s ");//create an array. This is the part I cant figure out. It creates the array, but I cant figure out how to get this data to "results" below.
int[] results = Stream.of(item).mapToInt(Integer::parseInt).toArray(); //converts the string array to an int array.
int sum = Arrays.stream(results).sum(); //calculates the sum of the array after its converted to an int to use in the monthly payment calculation.
bufferedWriter.write(line);
bufferedWriter.newLine();
}
bufferedWriter.write("Number of months of the loan: " String.valueOf(loanMonths));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
uj5u.com熱心網友回復:
您的輸入由交替的數字和非數字資料組成。split("\\s ")并且在與您將空格分割線之后,嘗試將所有字串轉換為int. 這將不可避免地導致NumberFormatException在運行時。
為避免這種情況,您需要filter()在流中添加 a 以確保僅決議由數字組成的字串。
而且由于您int[] results僅將其用作計算的第二個流的來源,因此sum您應該擺脫冗余。無需創建第二個流并在記憶體中分配未使用的陣列。
另一個錯誤是變數的范圍sum僅限于while回圈。根據您的輸入示例,一行最多只包含一個 digit。這沒有多大意義,我認為這不是你的意圖。
以下是解決這些問題的方法之一:
int sum = 0;
try(Stream<String> lines = Files.lines(inputFile.toPath())) {
sum = getSum(lines);
} catch (IOException e) {
e.printStackTrace();
}
請注意,try-with-resources是處理實作AutoCloseable. 當try塊的執行完成(正常或突然)時,所有資源將被關閉。
計算總和的邏輯:
public static int getSum(Stream<String> lines) {
return lines.flatMap(line -> Stream.of(line.split("\\s ")))
.filter(str -> str.matches("\\d "))
.mapToInt(Integer::parseInt)
.sum();
}
這基本上是問題的答案:
將 String 陣列轉換為 Int 陣列并求和
要修復代碼的其他部分,您必須清楚地了解您要實作的目標。這段代碼里有很多動作,你需要把它分成不同的方法,每個方法都有自己的職責。
寫入的代碼outputFile似乎與計算總和的程序無關。基本上,您創建的副本inputFile只有一行:"Number of months of the loan: " String.valueOf(loanMonths).
如果您堅持必須同時執行這些操作,例如,inputFile可能很大,那么可能會這樣做:
try(BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
if (!line.isBlank()) {
sum = Integer.parseInt(line.split("\\s ")[1]);
}
}
writer.write("Number of months of the loan: " String.valueOf(loanMonths));
} catch (IOException e) {
e.printStackTrace();
}
Note that in this case, Java 8 streams are not needed because a line can contain only a single value and there's nothing to process with a stream.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/435743.html
