I am trying to implement a registration panel with username password and email only. I put all the usernames and passwords in a text file "usernames.txt". But my problem is not working properly if user clicks the submit button and the username already exists nothing happens( it should display JOptionPane with text "Username already exists!" ). here is my code :
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("usernames.txt"));
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
String line = br.readLine();
StringBuilder sb = new StringBuilder();
Pattern usernameAndPasswordPattern = Pattern.compile("^(\\S+) (\\S+)$");
while (line != null) {
sb.append(line); //
sb.append(System.lineSeparator()); //
line = br.readLine();
}
String everything = sb.toString();
Matcher userNameAndPasswordMatcher = usernameAndPasswordPattern.matcher(everything);
//if username exists!
while (userNameAndPasswordMatcher.matches()) {
String username = userNameAndPasswordMatcher.group(1);
String password = userNameAndPasswordMatcher.group(2);
if (username.equals(usernameTextField.getText())) {
int result = JOptionPane.showConfirmDialog(null, "Username already exists!", "Registration incomplete", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
Here you can see a pic from my panel