luh luh - 1 month ago 4x
Java Question

BufferedReader won't read

i have a program that the username and password are in a text file and the text file looks like this:


and i have this code to read the file

try {
BufferedReader read=new BufferedReader(new FileReader("election_un_pass.txt"));
String line="";

while((line=read.readLine())!=null) {
String [] info=line.split(":");

if(info[0].matches(Login.uname) && info[1].matches(Login.pass)){
new Main();
} else {
JOptionPane.showMessageDialog(null, "Username or Password might not be correct");
} catch (Exception e1) {

every time i run my program, even tho the username and password that i entered are correct, the
Username or Password might not be correct
message will still appear and
new Main()
won't appear. please help thank you so much


You could:

  1. Learn how to debug in whatever IDE you are using. With debug mode you can execute the code one line at a time and see the values of the variables at each point.
  2. Increase the logging information when the error occurrs

For example you could print to the console/logs/dialog:

String msg = String.format("could not find user name: [%s] and password: [%s] in line: [%s]", Login.uname, Login.pass, line);

Obviously you shouldn't print the expected user name and password anywhere when an incorrect value is input for security reasons, but it might be a quick way to find out whats going wrong :) It could be as simple as trailing whitespace characters somewhere or incorrect case somewhere.