Heyoo! I’m creating a simple login form using the swing package in java and I am having trouble in checking if the username and password is correct or not.
this here is the code currently:
public void actionPerformed(ActionEvent e) {
try{
File user = new File("Usernames.txt");
File pass = new File("Passwords.txt");
FileReader frUsername = new FileReader(user);
FileReader frPassword = new FileReader(pass);
BufferedReader brUsername = new BufferedReader(frUsername);
BufferedReader brPassword = new BufferedReader(frPassword);
String username = brUsername.readLine();
String password = brPassword.readLine();
if (e.getSource() == btnLogin){
while(username != null && password != null){
if ((txtUsername.getText()).equals(username) && (new String (jpfPassword.getPassword()).equals(password))){
JOptionPane.showMessageDialog(null, "Welcome: " + username, "Login Successful",JOptionPane.INFORMATION_MESSAGE); //this is for testing purposes only
}
else{
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Unable to Login",0); //this is for testing purposes only
}
break;
}
}
brUsername.close();
brPassword.close();
}
catch(IOException err){
System.err.println("File not found.");
}
}
}
The idea is to have multiple accounts stored in the password and usernames files. for example the file content is:
Username.txt:
SampleUsername1
SampleUsername2
Password.txt:
SamplePassword1
SamplePassword2
If line 1 from the username file is "sampleUsername1" then the password for it is also from line 1 "samplePassword1" of the password file. if the user and password isn’t the same line or not in the file, it should give an "invalid" error. I know it is not secure to put passwords in a txt file but this is only for practice purposes as I am still learning how to code. Any kind of help and tips is really appreciated. Thanks!
You may also like…
- Crashed: com.apple.main-thread partial apply for closure #2
- Python simplekml CSV to KML with extended data and create folder based on group by column name and donut marking
- Hibarnate – Picking a fields as null even when object has that field value
- Merge Sorted Array without Extra Space
- how to add stripe like devider on React
- efficient data collection from API using R
- Update Spring Embedded Entity throws Stack Overflow Entity
- How To Get Query Result and Count Of Result In SQL
- inserting records in form subnmisstion is blank in phpmyadmin
- How to use artifactory query language inside a jenkins pipeline instead of having it call from a file
- Can't connect to Docker Hosted Postgres
- TypeError list indices must be integers or slices, not list
- Serverless deployment issue in different regions
- Is there a reliable alarm clock? Alarm Manager?
- Newtonsoft.Json 13.0.1 Serialize not getting lists inside of dictionary
- How to solve that A valid Mapbox access token is required to use Mapbox GL JS?
amazon-web-services android angular api arrays c# css dart dataframe django docker excel express firebase flutter html ios java javascript jquery json kotlin laravel linux list mongodb mysql node.js pandas php postgresql python python-3.x r react-native reactjs regex spring spring-boot sql sql-server string swift typescript vue.js