views:

203

answers:

2

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());
}
+2  A: 

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.

Vineet Reynolds
I have stored the details in the notepad file in this order (using tab). Is it correct?PIN AccountNo Balance1598 01-10-102203-0 950004895 01-10-102248-0 450009512 01-10-102215-0 1250006125 01-10-102248 85000
Yoosuf
@Yoosuf, it appears that you are using a TSV (Tab separated values) in your file. That assumption is not present in your code, hence the failure. You need to extract the PIN no from every line (i.e. from the strLine variable) and then compare it against the input PIN. Otherwise, you'll be comparing the input PIN against each line in the file, which is incorrect.
Vineet Reynolds
I need to retreive the balance for each PIN no as well. If you dont mind please show me the format by which I ought to store the details. Or if my current way is ok, then how should the code be to compare against each line. Thanks a lotttt!!
Yoosuf
A: 

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.

atamanroman