我正在為 Uni 開發一個專案,我想將檔案內容加載到稍后列印的本地陣列中。
當用戶選擇 1 來查看錢包時,我希望程式將文本檔案中的加密余額顯示到控制臺上。但是,我的代碼沒有這樣做。我對使用緩沖非常陌生,并且我對以前的分配進行了逆向工程,所以我的代碼一定是錯誤的。
我的文本檔案包含“BTC 1.3426 ETH 5.2519 XRP 158.5000”
請忽略所有 // 代碼并查看 和viewWallet()部分readCryptoBalance()。
import java.io.*;
import java.util.*;
public class Main {
static Scanner userInputInt = new Scanner(System.in);
static Scanner userInputString = new Scanner(System.in);
static Scanner userInputDouble = new Scanner(System.in);
public static void main(String[] args) { //throws Exception
int userChoice = getUserChoice();
switch (userChoice) {
case 1:
viewWallet();
break;
}
}
// This method asks and returns what the user wants to do
public static int getUserChoice() {
System.out.println("****************************************************************");
System.out.println("****************************************************************");
System.out.println("************ Crypto Wallet **********");
System.out.println("****************************************************************");
System.out.println("****************************************************************");
System.out.println("");
System.out.println("What do you want to do?");
System.out.println("(1) View wallet");
System.out.println("(2) Look up the balance of a given crypto");
System.out.println("(3) Add new cryptos");
System.out.println("(4) Remove an existing crypto");
System.out.println("(5) Update wallet");
System.out.println("****************************************************************");
System.out.print("Please enter your choice (1, 2, 3, 4, or 5): ");
return userInputInt.nextInt();
}
// This method is called for user choice 1
public static void viewWallet() {
double[] balance = readCryptoBalance();
System.out.println(balance);
}
// This method reads and returns the crypto balances from the file
public static double[] readCryptoBalance() {
double[] temp = new double [100];
int lineNumber =0;
try{
BufferedReader myFile = new BufferedReader (new FileReader("wallet.txt"));
String sCurrentLine;
while ((sCurrentLine = myFile.readLine()) != null){
temp[lineNumber] =Integer.parseInt(sCurrentLine.split("\t")[1]);
lineNumber ;
}
myFile.close();
} catch (IOException e){
System.out.println("I/O exception error when reading");
}
double[] balance = new double[lineNumber];
System.arraycopy(temp, 0, balance, 0, lineNumber);
return balance;
}
}
uj5u.com熱心網友回復:
您有一些錯誤(除了我在編輯中添加的缺少花括號)。
sCurrentLine.split("\t")- 這里有兩個錯誤。第一個錯誤是您需要轉義斜杠字符。因此,正確的表達方式應該是"\\t"。但是,您的檔案可能不是制表符分隔的。因此,對于以空格分隔的文本,您應該使用"\\s"、" "或"\\s "。如果您不確定有多少空格,最后一個運算式是最好的,因為它可以處理一個或多個空格。Integer.parseInt(...)- 這必須是Double.parseDouble()因為檔案中的值是浮點數,而不是整數。System.out.println(balance)insideviewWallet()不正確 - 該行顯示物件,而不是內容,因為陣列無法覆寫toString()。因此,您必須遍歷balance陣列并顯示其內容。最簡單的方法是這樣的:
for(double value : balance) {
System.out.println(value);
}
這將在單獨的行中顯示陣列中的所有值。如果陣列為空,它將完全跳過。
更正的代碼
import java.io.*;
import java.util.*;
public class Main {
static Scanner userInputInt = new Scanner(System.in);
static Scanner userInputString = new Scanner(System.in);
static Scanner userInputDouble = new Scanner(System.in);
public static void main(String[] args) { // throws Exception
int userChoice = getUserChoice();
switch (userChoice) {
case 1:
viewWallet();
break;
}
}
// This method asks and returns what the user wants to do
public static int getUserChoice() {
System.out.println("****************************************************************");
System.out.println("****************************************************************");
System.out.println("************ Crypto Wallet **********");
System.out.println("****************************************************************");
System.out.println("****************************************************************");
System.out.println("");
System.out.println("What do you want to do?");
System.out.println("(1) View wallet");
System.out.println("(2) Look up the balance of a given crypto");
System.out.println("(3) Add new cryptos");
System.out.println("(4) Remove an existing crypto");
System.out.println("(5) Update wallet");
System.out.println("****************************************************************");
System.out.print("Please enter your choice (1, 2, 3, 4, or 5): ");
return userInputInt.nextInt();
}
// This method is called for user choice 1
public static void viewWallet() {
double[] balance = readCryptoBalance();
for(double value : balance) {
System.out.println(value);
}
}
// This method reads and returns the crypto balances from the file
public static double[] readCryptoBalance() {
double[] temp = new double[100];
int lineNumber = 0;
try {
BufferedReader myFile = new BufferedReader(new FileReader("wallet.txt"));
String sCurrentLine;
while ((sCurrentLine = myFile.readLine()) != null) {
String[] tokens = sCurrentLine.split("\\s ");
temp[lineNumber] = Double.parseDouble(tokens[1]);
lineNumber ;
}
myFile.close();
} catch (IOException e) {
System.out.println("Error reading crypto balance: " e.getMessage());
}
double[] balance = new double[lineNumber];
System.arraycopy(temp, 0, balance, 0, lineNumber);
return balance;
}
}
免責宣告:我創建用于測驗的檔案的內容與 OP 提供的字串完全相同(減去雙引號)BTC 1.3426 ETH 5.2519 XRP 158.5000。因此,我的解決方案假定以空格分隔的字串在一行中。在每個數字后添加回車/換行符,以便為不同的加密貨幣值提供不同的行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/430619.html
上一篇:Hackerrank陣列操作和比較問題我被要求擔任初級軟體工程師職位
下一篇:Postgres:空值陣列
