Something like this should help. Read the comments in code:
public void actionPerformed(ActionEvent e) {
String username = txtUser.getText(); // Get entered User Name
String password = txtPass.getText(); // Get entered Password
boolean loginSuccess = false; // Flag to indicate success or failure.
// Make sure the User actually supplied the required data:
if (username.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(this, "<html>You <b>must</b> supply both "
+ "a User Name and a Password!", "Student File Not Found!",
JOptionPane.WARNING_MESSAGE);
return;
}
/* Open a reader. 'Try With Resources' is used here to
auto-close the reader and free resources. */
try (Scanner reader = new Scanner (new File("Student.txt"))) {
String line;
while (reader.hasNextLine()) {
line = reader.nextLine();
line = line.trim();
/* If the line is blank or if the line does not start with
"Username:" then skip past it. If it does start with
"Username:" then parse the line using the String#split()
method to get the actual name. Is it the desired name? */
if (!line.isEmpty() && line.startsWith("Username:") &&
line.split(":\\s*")[1].equalsIgnoreCase(username)) {
// Yes it is...read in the next line which would be the password:
String passwordLine = reader.nextLine();
/* Parse the Password line to retrieve the password. Is it
the desired password? */
if (passwordLine.split(":\\s*")[1].equals(password)) {
/* Yes it is... flag login as successful and exit
reading loop. */
loginSuccess = true;
break;
}
}
}
// Was login successfull?
if (loginSuccess) {
// Yes, it was...Inform User.
JOptionPane.showMessageDialog(this, "Login Successful!");
}
else {
// No, it wasn't...Inform User.
JOptionPane.showMessageDialog(this, "<html>Login <font color=red><b>"
+ "Failed!</b></font> Exiting...</html>");
System.exit(0); // Close Application!
}
}
catch (FileNotFoundException ex) {
// Trap FileNotFoundException and inform User:
JOptionPane.showMessageDialog(this, ex.getMessage(),
"Student File Not Found!", JOptionPane.WARNING_MESSAGE);
}
}
User name is not letter case sensitive but the Password is.
Update - Using a different file Reader:
This is the same code but utilizing a different reader. Here we use a BufferedReader in combination with a FileReader. Again, Try With Resources is used here to auto-close the reader and free resources when reading has completed. Make sure your Student.txt file is located within the project folder or provide a complete path to the Student.txt data file (ex: "C:/MyStudentFiles/Student.txt" or "C:\\MyStudentFiles\\Student.txt":
public void actionPerformed(ActionEvent e) {
String username = txtUser.getText(); // Get entered User Name
String password = txtPass.getText(); // Get entered Password
boolean loginSuccess = false; // Flag to indicate success or failure.
// Make sure the User actually supplied the required data:
if (username.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(this, "<html>You <b>must</b> supply both "
+ "a User Name and a Password!", "Student File Not Found!",
JOptionPane.WARNING_MESSAGE);
return;
}
/* Open a reader. 'Try With Resources' is used here to auto-close
the reader and free resources when done. */
try (BufferedReader reader = new BufferedReader(new FileReader("Student.txt"))) {
String line;
// Read in each line one by one...
while ((line = reader.readLine()) != null) {
line = line.trim();
/* If the line is blank or if the line does not start with
"Username:" then skip past it. If it does start with
"Username:" then parse the line using the String#split()
method to get the actual name. Is it the desired name? */
if (!line.isEmpty() && line.startsWith("Username:")
&& line.split(":\\s*")[1].equalsIgnoreCase(username)) {
// Yes it is...read in the next line which would be the password:
String passwordLine = reader.readLine();
/* Parse the Password line to retrieve the password. Is it
the desired password? */
if (passwordLine.split(":\\s*")[1].equals(password)) {
/* Yes it is... flag login as successful and exit
reading loop. */
loginSuccess = true;
break;
}
}
}
}
catch (IOException ex) {
// Trap FileNotFoundException and inform User:
JOptionPane.showMessageDialog(this, ex.getMessage(),
"Student File Read Error!", JOptionPane.WARNING_MESSAGE);
}
// Was login successfull?
if (loginSuccess) {
// Yes, it was...Inform User.
JOptionPane.showMessageDialog(this, "Login Successful!");
}
else {
// No, it wasn't...Inform User.
JOptionPane.showMessageDialog(this, "<html>Login <font color=red><b>"
+ "Failed!</b></font> Exiting...</html>");
System.exit(0); // Close Application!
}
}
If you are actually using the code above then all should work.