使用文本文件的Java登录屏幕 [英] Java login screen using a textfile

查看:53
本文介绍了使用文本文件的Java登录屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Java应用程序,我需要从文本文件中获取用户PIN.我在下面使用了以下代码,但是它不能正常工作.任何人都可以帮我一下吗.....

I am creating a java application and I need to get the user PINs from the text file. I used the following code below, but is it not working properly. Could anyone please help me out soon.....

    String typedPIN="";   
    Menus obj1=new Menus();
    BufferedReader getIt=new BufferedReader(new InputStreamReader(System.in));
    String userPIN="";
    try{
        BufferedReader br = new BufferedReader(new FileReader(new File("D:\\Studies\\BCAS\\HND\\Semester 1\\Programming Concepts\\Assignment\\AccountPIN.tab")));
        String strLine=null                    ;
        System.out.println("Enter PIN");
        userPIN=getIt.readLine();  
        while ((strLine = br.readLine()) != null)   {          
            if(userPIN.equals(strLine)){
                System.out.println("You have login!");
                obj1.MainMenu();
            }
        }    
    }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
    }
 }   

推荐答案

假设这是您的输入数据

PIN AccountNo Balance
1598 01-10-102203-0 95000
4895 01-10-102248-0 45000
9512 01-10-102215-0 125000
6125 01-10-102248 85000

您需要将每一行拆分成相应的部分,您可以使用 Scanner 类可以做到这一点,因为它将使您将Pin/帐号提取为Strings,将余额提取为Double/Integer.

You will need to split each line into its consituent parts, you could use the Scanner class to do this, as it will let you extract the pin / account number as Strings and the balance as a Double/Integer.

此刻,您正在将用户输入与整行进行比较,因此您需要输入图钉1598 01-10-102203-0 95000而不是1598才能登录.

At the moment you are comparing the user input against the whole line, so you would need to enter a pin 1598 01-10-102203-0 95000 rather than 1598 in order to login.

我建议您将其分为两种方法,一种在给定文件时返回

I suggest you split this into to two methods, one which when given a file returns a Collection of Account objects, and another which handles the login.

您可以重新编写while循环,以使您在未获得有效密码的情况下给出有用的错误消息,例如

You could re-write your while loop to enable you to give a useful error message if you don't get a valid pin, e.g.

final File data = new File("D:\\Studies\\BCAS\\HND\\Semester 1\\Programming Concepts\\Assignment\\AccountPIN.tab");
Account userAcc = null;
for (Account acc : getAccounts(data)) {          
    if(userPIN.equals(acc.getPin())){
        userAcc = acc;
    }
}
if (userAcc == null) {
    obj1.MainMenu();
} else {
    // display error
}

这篇关于使用文本文件的Java登录屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆