0

I need to retrieve the PIN from a notepad file (below) and check it with the PIN which the user has typed. I have tried this for days, but so far the solution that I have come up with gives me the correct output only when I type the full row (i.e. 1598 01-10-102203-0 95000). Also it displays the "Invalid PIN" for each and every record.

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

Output - You have login!

    Invalid PIN    
    Invalid PIN
    Invalid PIN
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!");
        } else {
            System.out.println("Invalid PIN!");
        }
    }
    //Close the input stream
    in.close();
} catch (Exception e) {//Catch exception if any
    System.err.println("Error: " + e.getMessage());
}
5
  • I need to finish this program soon, anybody please help. !!! Commented Sep 17, 2010 at 4:07
  • 3
    possible duplicate of Java login screen using a textfile Commented Sep 17, 2010 at 4:11
  • well i corrected the conditions here and now it works at least when I type the whole line (earlier it never worked)! i couldnt get Commented Sep 17, 2010 at 4:18
  • 1
    I advise you to click on trashgod's link. Commented Sep 17, 2010 at 4:23
  • Reformatted code; please revert if incorrect. Commented Sep 17, 2010 at 5:08

1 Answer 1

3

An essential element of Jon Freedman's excellent answer, which you should consider accepting, is that you must break up the incoming line of text into its component parts in order to compare them to what is typed. Here's one approach:

String line = "1598 01-10-102203-0 95000";
for (String s : line.split(" ")) {
    System.out.println(s);
}

This produces the following output:

1598
01-10-102203-0
95000

Addendum:

while ((strLine = br.readLine()) != null) {
    String[] a = strLine.split(" ");
    // now the array a contains the three parts
    ...
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for replying this, but this code is working only for one PIN, because it is reading each number in order (one at a time). My question is, is it possible to retrieve only the first four digits and store in an array? Your reply is greatly appreciated...
@Yoosuf: In your while loop, you can use strLine.split(" ") to get the PIN from each input line.
I made the code lyk this, while ((strLine.split(" ") = br.readLine()) != null) but its displaying an unexpected type error, required variable found value. I kept the for loop inside
@Yoosuf: I looks like you got it straightened out. It's generally better not to try to squeeze too much onto one line.
then its giving me an unexpected type error. If you dont mind could you please type it how it should be thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.