views:

161

answers:

1

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

Assuming this is your input data

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

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.

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.

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
}
Jon Freedman
i dont get how i should create an Account method (Account userAcc=null). Please help me I have tried this for days now with no result
Yoosuf
Do you mean you don't know how to implement `getAccounts`? or how to create an `Account` class?
Jon Freedman
exactly i dont knw wat to code in the Account class?
Yoosuf