Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Bryan Beasman
Bryan Beasman
3,435 Points

Feedback Request: My first Java Program: An Authentication Program

Hello all. I am new to the Java Programming language and I would classify myself as a programming novice. I created a simple console authentication program after finishing Java Basics and doing a little further research on my own. I was wondering if I could get some feedback on my code (does it look good? am I using best practices? are there better ways to write this program at this stage in my learning?)

Please see code below:

import java.io.Console;

public class Auth{

public static void main(String[] args) {

  Console console = System.console();

  //Declare Variables
  String name;
  String username;
  String usernameCheck;
  boolean isUsername;
  String password;
  String passwordCheck;
  boolean isPassword;
  int triesLeft;

  //Prompt User for Name
  name = console.readLine("Hello, what is your name? ");

  //Prompt user for a username and a password
  username = console.readLine("\n%s, please choose a username: ",name);
  password = console.readLine("Now, please choose a password: ");

  console.printf("-----\nGreat, %s, you now have a username and a password.\n-----\n",name);

  //Verify that the user remembers his/her login credentials (do while loops)
  triesLeft = 6;
  do{
    usernameCheck = console.readLine("Please retype your username: ");
    isUsername = usernameCheck.equals(username);

    triesLeft = triesLeft - 1;

    if(isUsername == false){
      console.printf("-----\nI'm sorry, %s, that is not your username. ",name);
      console.printf("You have %d more tries.\n-----\n",triesLeft);
    }

    if(triesLeft == 0){
      console.printf("Exiting Program...\n\n");
      System.exit(0);
    }
  } while(isUsername == false);

  console.printf("\nGood! %s is your username.\n\n",username);

  triesLeft = 6;
  do{
    passwordCheck = console.readLine("Please retype your password: ");
    isPassword = passwordCheck.equals(password);

    triesLeft = triesLeft - 1;

    if(isPassword == false){
      console.printf("-----\nI'm sorry, %s, that is not your password. ",name);
      console.printf("You have %d more tries.\n-----\n",triesLeft);
    }

    if(triesLeft == 0){
      console.printf("Exiting Program...\n\n");
      System.exit(0);
    }
  } while(isPassword == false);

  console.printf("\nGood! %s is your password.\n\n",password);

  console.printf("-----\nProgram Unlocked\n-----\n\n\n");

}

}