private static BufferedReader in ; private static boolean foundLogin ; private s
ID: 3613232 • Letter: P
Question
privatestatic BufferedReader in;
private static boolean foundLogin;
privatestatic voidsearchLogin() {
try{
in =new BufferedReader(newFileReader(newFile(filename)));
}catch(IOExceptione){
e.printStackTrace();
}
String lineContent =null;
int currentLine = 0;
foundLogin = false;
while(true){
currentLine++;
try{
lineContent =in.readLine();
}catch(IOExceptione){
e.printStackTrace();
break;
}
if(lineContent == null){
break;
}
if(lineContent.indexOf(searchLogin)==-1){
continue;
}else{
foundLogin = true;
break;
}
}
if(!foundLogin)
//JOptionPane.showMessageDialog(null, "Sorrycould not find that login name");
foundLogin = false;
try{
in.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
Explanation / Answer
You can split a line containing"namepassword" on the whitespace using Arrays.split(" ") Stringline=in.readLine(); String[] split=line.split(" "); String name=split[0]; String password=split[1]; Also, I was wondering why everythingwas static. If you are designing this program yourself, youmight consider making a class to search for logins, instantiatingone, and using that. (If you are implementing methodsdesigned by your professor, we would not want to be to differentfrom his code - just use the split[] method to find the separatename and password). You could also have your searchLogin()method return a boolean - whether the login is found or not. I would design it as public booleansearchLogin(String in_name,String in_pass), wherein_name is the entered name andin_pass is the entered password. Then compare those against the name and password found by splittingthe line above - and return true if they match. Forexample: public booleansearchLogin(String in_name,String in_password) { try { in=newBufferedReader(newFileReader(new File(filename)); String line=null; String name=null; String password=null; while(true) { line=in.readLine(); String[] split=line.split(" "); name=split[0]; password=split[1]; if(in_name.equals(name) &&in_password.equals(password)) { returntrue; } } } catch(Exceptionex) { e.printStackTrace(System.err); } // all names arechecked, or file is not found; either way, return false returnfalse; } When checking user input, you couldcall searchLogin() with the user's input, then handle the returnedboolean (denoting whether the user was found or not). Additionally, if you wanted to note different possibilities, suchas 1) correct name/password 2) incorrect password 3) incorrectname, you could return an int instead of a boolean denoting which outcome wasfound. This is to both organize your code and improve its reusability -being able to use certain parts of it from different areas of yourprogram as needed. _____ Apologies for the differingcolorcoding for syntax. If you provide the colors you usefor operators, classes, and keywords, I can colorcodethose parts with my program if you wish. As it is, I thought thatsome color coding was better thannone.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.