Java 读取和写入txt文件

Java 读取和写入txt文件,java,file,if-statement,printwriter,Java,File,If Statement,Printwriter,我正在编写一个代码,读取Banking.txt文件并请求输入userID和pin,然后提示用户进行取款、存款或检查余额。余额将写入Records.txt。程序在用户输入pin后终止,我不知道如何让它继续。请帮忙 public static void main(String[] args) throws FileNotFoundException { File fil1 = new File("Banking.txt"); File fil2 = new File("Records

我正在编写一个代码,读取Banking.txt文件并请求输入userID和pin,然后提示用户进行取款、存款或检查余额。余额将写入Records.txt。程序在用户输入pin后终止,我不知道如何让它继续。请帮忙

public static void main(String[] args) throws FileNotFoundException {
    File fil1 = new File("Banking.txt");
    File fil2 = new File("Records.txt");
    Scanner sc1 = new Scanner(fil1);
    Scanner s1 = new Scanner(fil2);
    String fname;
    String mname;
    String lname;
    String uid = null;
    int pin = 0;
    double balance = 0;
    java.io.PrintWriter output = new java.io.PrintWriter(fil2); 
    while (sc1.hasNext()) {
        fname = sc1.next();
        mname = sc1.next();
        lname = sc1.next();
        uid = sc1.next();
        pin = sc1.nextInt();
        balance = sc1.nextDouble();
        BankRecord person = new BankRecord(fname,mname,lname,uid,pin,balance);
        System.out.print(pin);
    }

Scanner input = new Scanner(System.in); 
System.out.print("Please enter a user ID: ");
String entereduid = input.nextLine();
if(entereduid.equals(uid)){
System.out.print("Please enter a Pin #: "); 
String enteredpin = input.nextLine();
if(enteredpin.equals(pin)){
Scanner input2 = new Scanner(System.in);
System.out.print("press 1 to check balance, 2 for a deposit, or 3 for a withdrawal: "); 
int ans = input.nextInt();
if(ans == 1){
System.out.print(balance);
output.println("The balance is now "+balance+"$.");
}
else if(ans == 2){
    System.out.print("Your current balance is: " + balance+". Please enter an amount to deposit: ");
    double dep = input.nextDouble();
    balance = balance + dep;
    System.out.print("You deposited "+dep+"$. Your new balance is: " + balance+"$.");
    output.println("The balance is now "+balance+"$.");
}
else if(ans == 3){
    System.out.print("Yor current balance is: "+balance+". Please enter an amount to withdrawl: ");
    double wit = input.nextDouble();
    balance = balance - wit;
    System.out.print("You withdrew "+wit+"$. Your new balance is: "+balance+"$.");
    output.println("The balance is now "+balance+"$.");

}}}}}

您正在以字符串形式从输入中读取pin,但从文件中以整数形式读取pin;所以整数引脚!=别针。您的pin永远不会被识别为正确,因此您的程序会跳到最后

尝试
新建扫描仪(System.in).nextInt()
将输入读取为整数

所以我要改变这一行:

String enteredpin = input.nextLine();
为此:

Integer enteredpin = input.nextInt();

如果我理解正确,您想将“person”对象(即BankRecord的实例)写入文件吗?因为我不知道那个类中有什么方法,所以我假设有一个合适的toString()重写。我还假设您正在尝试写入主目录

        Path dir = Paths.get(URI.create("file:///"+System.getProperty("user.home")+System.getProperty("file.separator")+"BankRecords.txt"));
        try (BufferedWriter bfr = Files.newBufferedWriter (dir, Charset.forName("UTF-8"))) {
            String contents = person.toString();
            bfr.write(contents, 0, contents.length());
        }
        catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        }

考虑使用一些反映您的逻辑的方法-如果需要,先将逻辑写在纸上一点缩进也会有帮助您制作新的
扫描仪
实例的任何特定原因?使用输入实例有什么问题?@lane-你说得对。
新扫描仪…
只是样板文件-如果已经创建了相同的扫描仪实例,最好使用相同的扫描仪实例。将更新