Java Program to Illustrate how User Authentication is Done

This is a Java Program to Illustrate how User Authentication is Done.

Enter username and password as input strings. After that we match both strings against given username and password. If it matches then Authentication is successfull or else Authentication fails.

Here is the source code of the Java Program to Illustrate how User Authentication is Done. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class User_Authentication 
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         String username, password;
  7.         Scanner s = new Scanner(;
  8.         System.out.print("Enter username:");//username:user
  9.         username = s.nextLine();
  10.         System.out.print("Enter password:");//password:user
  11.         password = s.nextLine();
  12.         if(username.equals("user") && password.equals("user"))
  13.         {
  14.             System.out.println("Authentication Successful");
  15.         }
  16.         else
  17.         {
  18.             System.out.println("Authentication Failed");
  19.         }
  20.     }
  21. }


$ javac
$ java User_Authentication
Enter username:user
Enter password:user
Authentication Successful
Enter username:abcd
Enter password:1234
Authentication Failed

Sanfoundry Global Education & Learning Series – 1000 Java Programs.


Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.