-1

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

election:12345

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");
        }
        Login.txtUName.setText("");
        Login.txtPassword.setText("");
     }
} catch (Exception e1) {
    e1.printStackTrace();
}

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.

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
luh
  • 3
  • 4
  • Please check the Login.uname and Login.pass values it seems they are not matching. – Vural Sep 07 '16 at 21:38
  • 1
    Sure that you don't want to use `String.equals`? And what are the values of `Login`? – Marvin Sep 07 '16 at 21:40
  • @VuralAcar oh they are actually matched and the Login.uname and Login.pass are initialized to the txtUName.getText() and txtPassword.getText() but because of your suggestion i just directly used the txtUName.getText() and txtPassword.getText(). thank you so much!!!! – luh Sep 07 '16 at 21:45
  • @Marvin `txtUName.getText()` and `txtPassword.getText()` but i just directly used them now and it's now fixed thank you so much!!! – luh Sep 07 '16 at 21:47
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Sep 11 '16 at 08:12

2 Answers2

0

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.

Tom Hanley
  • 1,252
  • 11
  • 21
0

Are you certain that you want to use String.matches(regex) for your comparisons? The argument is taken as a regular expression, so any regex meta-characters in your Login.uname or Login.pass values would be interpreted as regex. This means your match might work in ways you don't expect.

I suspect you really want to say:

if (info[0].equals(Login.uname) && info[1].equals(Login.pass)) {

instead.

SusanW
  • 1,550
  • 1
  • 12
  • 22