Jpixta Jpixta - 6 months ago 34
Java Question

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.");
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 (;
    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.