I need to create a system that checks a file for the username and password and if it is correct, it says whether or not in a label. So far I have been able to simply make one username and password equal to the variable, but need to link it to a file somehow. I am a noob programmer so lots of help is appreciated. Here is what I have under my authenticate button.
String pass;
String user;
user = txtUser.getText();
pass = txtPass.getText();
if(pass.equals("blue") && user.equals("bob") ){
lblDisplay.setText("Credentials Accepted.");
}
else{
lblDisplay.setText("Please try again.");
}
You will need to use java.util.Scanner
for this issue.
Here is a good login program for the console:
import java.util.Scanner; // I use scanner because it's command line.
public class Login {
public void run() {
Scanner scan = new Scanner (new File("the\\dir\\myFile.extension"));
Scanner keyboard = new Scanner (System.in);
String user = scan.nextLine();
String pass = scan.nextLine(); // looks at selected file in scan
String inpUser = keyboard.nextLine();
String inpPass = keyboard.nextLine(); // gets input from user
if (inpUser.equals(user) && inpPass.equals(pass)) {
System.out.print("your login message");
} else {
System.out.print("your error message");
}
}
}
Of course, you will use Scanner scanner = new Scanner (File toScan);
but not for user input.
Happy coding!
As a last note, you are at least a decent programmer if you can make Swing components.
Code
import java.util.Scanner;
public class LoginMain {
public static void main(String[] args) {
String Username;
String Password;
Password = "123";
Username = "wisdom";
Scanner input1 = new Scanner(System.in);
System.out.println("Enter Username : ");
String username = input1.next();
Scanner input2 = new Scanner(System.in);
System.out.println("Enter Password : ");
String password = input2.next();
if (username.equals(Username) && password.equals(Password)) {
System.out.println("Access Granted! Welcome!");
}
else if (username.equals(Username)) {
System.out.println("Invalid Password!");
} else if (password.equals(Password)) {
System.out.println("Invalid Username!");
} else {
System.out.println("Invalid Username & Password!");
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With