Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to code a very simple login system with java

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.");
}     
like image 482
Jpixta Avatar asked May 18 '13 19:05

Jpixta


2 Answers

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.

like image 154
Galen Nare Avatar answered Sep 28 '22 05:09

Galen Nare


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!");
    }

}

}
like image 21
Armando Lee Avatar answered Sep 28 '22 05:09

Armando Lee