Jump to content

wok

Community Member
  • Posts

    236
  • Joined

  • Last visited

  • Donations

    0.00 GBP 

Community Answers

  1. wok's post in Java help was marked as the answer   
    Here's an example on how to accept a specific hardcoded user and pass:
    package login_out_code; import java.util.*; import java.io.*; public class Login_out_code {     public static void main(String[] args) {         String username = "username";         String password = "passw0rd";         System.out.println("Hello! Please login to access our system");         System.out.println("Please type the username: ");         String ass;         try {             java.io.BufferedReader butt = new java.io.BufferedReader(new java.io.InputStreamReader(System.in ));             ass = butt.readLine();             if (!ass.equals(username)) {                 System.out.println("Invalid Username.");                 System.exit(0);             }         } catch (Exception e) {             System.out.println("Invalid Username.");             int breakp = 43;         }         System.out.println("Now, please type the password: ");         String bacon;         try {             java.io.BufferedReader pig = new java.io.BufferedReader(new java.io.InputStreamReader(System.in ));             bacon = pig.readLine();             if (!bacon.equals(password)) {                 System.out.println("Invalid Password.");                 System.exit(0);             }         } catch (Exception e) {             System.out.println("Invalid Password, please try again.");             int breakp = 43;         }         System.out.println("Welcome to the the system!");     } }  
     
    It's just an example since if you have the username and password hardcoded, anyone with access to the files can read them, you would want to use a database to store the passwords encrypted.
     
    I have added two "if" conditionals for the ass and bacon vars which are the user and pwd you enter on the console, if they don't match the hardcoded values (the username and password string vars on the top) the program just exit, if they match the code execution continues as normal. To do the "please try again" thing you would need a while loop, let me know if you want me to show you how to do that.
×
×
  • Create New...