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());
}
}
Assuming this is your input data
You will need to split each line into its consituent parts, you could use the
Scannerclass 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 95000rather than1598in order to login.I suggest you split this into to two methods, one which when given a file returns a
CollectionofAccountobjects, 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.