By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,648 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,648 IT Pros & Developers. It's quick & easy.

Simple User/Pass Access

P: 2
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  3. public class Test {
  4.     public static void main(String[] args) {
  5.         Scanner input = new Scanner(;
  6.         String username, tempuser;
  7.         username = "testuser";
  9.         {System.out.println("Please enter your username.");
  10.         tempuser = input.nextLine();
  11.         if (!tempuser.equals(username)){
  12.             System.out.println("Wrong username.");
  13.             Test.main(null);}
  14.         else 
  15.             Test.Access();
  16.         }
  17.     }
  19.     public static void Access() {
  20.         Scanner input2 = new Scanner(;
  21.         String password, temppassword;
  22.         password = "testpass";
  24.     System.out.println("Please enter your password.");
  25.         temppassword = input2.nextLine();
  27.         if (!temppassword.equals(password)){
  28.             System.out.println("Wrong password.");
  29.             Test.main(null);}
  30.         else
  31.             System.out.println("Access Granted!");}
  34. }
Pastebin of JAVA Code for easier reading.

I am a newbie to Java and have been learning and testing. My question is this, is the code above proper for a simple protected program in its current state?
Mar 22 '12 #1
Share this Question
Share on Google+
3 Replies

P: 13,264
Typically password are stored in a database. Actually their digests are stored in the database. A digest is a fixed length string created by applying some algorithm to a string in such a way that it is practically impossible to deduce the password given it's digest.
The authentication is then done by comparing digests rather than comparing the actual passwords.
Mar 22 '12 #2

P: 2
Just for the sake of argument and to stay within my limits, this program is to be used by only me and only on my computer, and therefore I would have the only password. Would the above work?
Mar 22 '12 #3

P: 13,264
Your problem would be that anyone who has access to your program (even the compiled .class files) will be able to open the .class file and see both the user name and password in it.
Mar 22 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.