我有一个任务,我必须做一个简单的银行应用程序。我需要将客户信息存储在名字,客户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