【问题标题】:Reading and writing to txt files读取和写入 txt 文件
【发布时间】:2016-02-10 02:07:26
【问题描述】:

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

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+"$.");

}}}}}

【问题讨论】:

  • 考虑使用一些反映您的逻辑的方法 - 如果需要,请先将逻辑写在纸上
  • 稍微缩进也会有所帮助

标签: java file if-statement printwriter


【解决方案1】:

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

尝试new Scanner(System.in).nextInt() 将输入读取为整数。

所以我会改变这一行:

String enteredpin = input.nextLine();

到这里:

Integer enteredpin = input.nextInt();

【讨论】:

  • 创建一个新的Scanner 实例有什么特别的原因吗?改用输入实例有什么问题?
  • @lane - 你是对的。 new Scanner... 只是样板文件 - 如果它已经创建,最好使用相同的 Scanner 实例。会更新
【解决方案2】:

如果我理解正确,您想将“人”对象(即 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());
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2013-02-02
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    相关资源
    最近更新 更多