首先請原諒我的錯誤輸入錯誤和張貼錯誤,因爲我是新手,並且仍在學習如何使用它。無法讀取Java文件中的數字和值?
我應該創建一個代碼,詢問用戶他是否想要存入或提取,然後要求他輸入文件名,然後使用裏面的信息添加到帳戶;無論如何,這段代碼大約有25%完成了,而且我在測試代碼時遇到了麻煩,因爲它在詢問我輸入時似乎沒有讀取文件的名稱,您是否知道可能會導致此問題的原因?任何建議或反饋?
import java.io.*;
import java.util.*;
public class read {
public static void main(String [] args) throws FileNotFoundException {
Double currentbalance = 500.00;
System.out.println("To deposit enter 1");
System.out.println("To Withdrwawl enter 2");
Scanner depositOrWithdrawl = new Scanner(System.in);
int choose = depositOrWithdrawl.nextInt();
if(choose == 1) {
double currentBalance = 500.00;
System.out.println("your current balance is: $"+ currentBalance);
System.out.println("Please enter the file name to deposit: ");
Scanner deposit = new Scanner(System.in);
String depositAmount = deposit.nextLine();
File depositFile = new File(depositAmount);
int nextLine = 1;
while(deposit.hasNextLine()){
double numbers = deposit.nextDouble();
System.out.println("You are about to add: $"+ numbers + " To your Balance");
currentBalance = currentBalance + numbers;
System.out.println("Your current balance is: $" + currentBalance);
nextLine++;
}
}
}
}
順便說一句,如果你告訴我如何包含代碼的前幾行使用計算器正確
你認爲哪一行應該從文件中讀取? – shmosel
@shmosel感謝您的快速回復。該文件由雙數數字組成,可以看作每行中的數字。所以代碼必須讀取所有行 –
關於你的[即將被刪除的問題](http://stackoverflow.com/questions/42775060/how-to-read-a-txt-file-in-java-and -store-its-values?noredirect = 1),你已經在這裏問過這個問題並得到了一個*詳細的答案。對不起,直言不諱,但請付出一些努力學習Java。 –