Creating a login page in java console application using a txt file

痴心易碎 提交于 2020-01-06 15:19:41

问题


I am creating a java console application and i need to use a notepad (txt) file to store PIN, AccountNo and Balance. When the user enters the PIN (from BufferedReader) it should get validated from the notepad file and login to thge system. However, the code fragment which I used below only displays Enter PIN, and when i click on Enter, the program exits. Could you please tell me what is the mistake which I have done here?

BufferedReader getIt = new BufferedReader(new InputStreamReader(System.in));
String userPIN = "";
try {
    // Open the file that is the first command line parameter
    FileInputStream fstream = new FileInputStream(
        "D:\\Studies\\BCAS\\HND\\Semester 1\\Programming Concepts\\Assignment\\AccountInfo.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    //Read File Line By Line
    System.out.println("Enter PIN");
    userPIN = getIt.readLine();

    while ((strLine = br.readLine()) != null) {
        // Print the content on the console#    
        if (userPIN.equals(strLine)) {
            System.out.println("You have login!");
        }
    }
    //Close the input stream
    in.close();
} catch (Exception e) {//Catch exception if any
    System.err.println("Error: " + e.getMessage());
}

回答1:


There are quite a few reasons why the program can simply terminate, or appear to be successful in program execution (and hence terminate successfully without doing any useful work):

  • The contents of the file are read, but if the end of the stream has been reached, then the code simply does not handle the case gracefully (or even consider handling it). Specifically, the line while ((strLine = br.readLine()) != null) can result in a scenario where the body of the loop is not executed.
  • The else condition is absent inside the body of the while loop. If none of the entries provided by the user input match the contents of the file, the code does not handle the condition, and the result is ambiguous. For all practical purposes, it appears that login is successful if the provided PIN is not present in the file.
  • Avoid unnecessary decoration of Readers and Streams. BufferedReader in = new BufferedReader(new FileReader("foo.in")); is sufficient to read character data from files, and it does provide the read and readline methods to use.

PS: It is recommended that you learn to program defensively, especially when dealing with files and streams. That would help isolating cases in your code that would result in ambiguious/vague conditions.




回答2:


File.ReadAllLines would be more convenient in your case.. Wouldn't be wrong to introduce a user class either if your going the good ol' oop way.

If you want to do this by yourself, StreamReader (which takes FileInfo) is a good way to go. Try to create it by using the using statement (:)) which makes cleans up after your done.



来源:https://stackoverflow.com/questions/3649207/creating-a-login-page-in-java-console-application-using-a-txt-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!