Java code For Passwords
Hi, I'm new to this forum and to java programming, I am takin a course on java in school and I was wondering if anyone could provide me with a very simple code that would allow me to add a password to my java code so when someone trys to run it they need the password. It just needs to be very basic 1 user name 1 password.
Im sorry if this has been asked and answered, and if this is in the wrong place, plz feel free to move it. Thx in advanced
Try this simple program.
import java.io.*;
class TestPassword {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
System.out.println ("Enter your username");
String user = br.readLine ();
System.out.println ("Enter your password");
String password = br.readLine ();
if (!"scott".equals (user) && "tiger".equals (password)) {
System.out.println ("Sorry, username unknown or password wrong");
System.exit (1);
}
System.out.println ("Hello, world!");
}
}
Sample run:
C>java -cp . TestPassword
Enter your username
scott
Enter your password
tiger
Hello, world!
Thank you for the Java code, it works, the only problem is I can put in any user name and password and the text will come up, is this suppose to be? or did i forget to program something? also is there any way i could make it look like the way that you would log onto this site? instead of just putting it in, in the console