我有一個任務,我必須做一個簡單的銀行應用程序。我需要將客戶信息存儲在名字,客戶ID和餘額的文件中。然後我需要創建一個菜單選項來檢查餘額,退出,存款和退出。我目前遇到的問題是在用戶想要檢查餘額時試圖顯示餘額。餘額是在我的文件的第四行,我不知道如何只顯示第四行,然後能夠使用該數字來添加或減去一個數字。我正試圖在switch語句中的case 1中執行此操作。任何提示將不勝感激。從java讀取文件?
import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.*;
public class bank {
public static void main(String[] args) throws IOException {
String fileName; // stores the name of the file
String bankCustomer; // used to display the customers bank information
int menuOption = 0; // allows the user to enter a number so they can select what they want to do
Scanner keyboard = new Scanner(System. in);
// enter the file name to see customer information
System.out.print("enter file name ");
fileName = keyboard.nextLine();
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
// read customer information from file i need this
while (inputFile.hasNext()) {
bankCustomer = inputFile.nextLine();
System.out.println(bankCustomer);
}
System.out.println("Please enter the number for the following:\n 1) Balance \n 2) Deposit + \n 3) Withdrawal \n 4) Quit ");
menuOption = keyboard.nextInt();
switch (menuOption) {
case 1:
// need to show balance how do i do that?
while (inputFile.hasNext()) {
bankCustomer = inputFile.nextLine();
System.out.println(bankCustomer);
}
break;
default:
System.out.println("Invalid choice");
}
inputFile.close();
}
}
我們還沒有覆蓋陣列但在課堂上,所以我在尋找一種方式來做到這一點不array –
@RishiDholliwar你有沒有講過創建類?如果是這樣,請創建包含所有這些字段的'User'類並填充它。 –
@RishiDholliwar檢查出更新的答案 – owaism