views:

115

answers:

1

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

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
    ...
}
trashgod
+1 - I'm sure we'll get there in the end...
Jon Freedman
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
@Yoosuf: In your `while` loop, you can use `strLine.split(" ")` to get the PIN from each input line.
trashgod
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
@Yoosuf: I looks like you got it straightened out. It's generally better not to try to squeeze too much onto one line.
trashgod
then its giving me an unexpected type error. If you dont mind could you please type it how it should be thanks
Yoosuf
@Yoosuf: I've elaborated above.
trashgod
i mean in the while loop to use strLine.split(" "). Should it be, While(strLine.split(" ")=br.readLine()). Then it is giving an error
Yoosuf
@Yoosuf: No, your `while` clause is OK; the `split` goes inside the loop, as I've shown. Once you have the array, you can compare the desired element to `userPIN`. Read about array access in your text.
trashgod