java - Reading and comparing strings in a text file, variables not initialized -
i'm getting error
the local variables name , password may not have been initialized,
for if-statement. these errors go away if change second string in parentheses in quotes, or if set variables 0, need change them int
, need them string
.
i'm trying compare username , password text file newly input username , password. program should quit after 3 bad attempts put system.out.println("goodbye!");
in wrong place too.
public static void main(string[] args) { int numberofattempts = 1; string filename = "logins.txt"; scanner inputstream = null; string name, password, line, secondname; string secondpassword; { system.out.println("please enter username: "); scanner keyboard = new scanner(system.in); secondname = keyboard.nextline(); system.out.println("please enter password: "); secondpassword = keyboard.nextline(); try { inputstream = new scanner(new file(filename)); } catch(filenotfoundexception e) { system.out.println("error opening file " + filename); system.exit(0); } while (inputstream.hasnextline()) { line = inputstream.nextline(); } if ((name.equalsignorecase(secondname))&& (password.equalsignorecase(secondpassword))) {system.out.println("welcome!"); numberofattempts = 4; } else system.out.println("invalid. please try again."); numberofattempts++; } while ( numberofattempts <4); system.out.println("goodbye!"); inputstream.close(); } }
you never initialize name
, password
, of course you'll error.
you initialize secondname
, secondpassword
, condition checks name
, password
not initialized.
Comments
Post a Comment