2 Replies Latest reply: Mar 20, 2014 5:05 PM by marlin RSS

    Help reviewing basic program for class

    48b6e975-5b6a-4c50-bb38-e170132ac63c

      Hey, everybody. Could you take a look at this program for my introductory Java class at the community college? I'm just wondering if there are any fixes I can make to make the program better. I am a beginning programmer, and this program uses most of my skills, so no super-technical jargon, please.

       

       

      import java.util.Scanner;

       

       

      public class PasswordChecker {

              public static void main(String[] args) {

                      System.out.println("Enter a password: ");

                      Scanner input = new Scanner(System.in);

                      String password = input.next();

                      float score = 0;

                      char c;

                      int cap = 0;

                      int low = 0;

                      int numbers = 0;

                      int chars = 0;

                      boolean check = false;

                      if(password.length() >= 6)

                              score++;

                      //System.out.println(score + " length");

       

       

                      char[] caps = {'A', 'B', 'C', 'D', 'E', 'F', 'G',

                                      'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',

                                      'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',

                                      'X', 'Y', 'Z'};

                      char[] lows = {'a', 'b', 'c', 'd', 'e', 'f', 'g',

                                      'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',

                                      'p', 'q', 'r', 's', 't', 'u', 'v', 'q',

                                      'x', 'y', 'z'};

       

       

                      char[] ints = {'0', '1', '2', '3', '4', '5', '6',

                                      '7', '8', '9'};

                     

                      String[] passwords = {"password", "123456", "12345678",

                      "qwerty", "baseball", "football", "letmein",

                      "111111", "superman", "master", "harley",

                      "drowssap", "654321", "87654321", "ytrewq",

                      "llabesab", "llabtoof", "niemtel", "111111",

                      "namrepus", "retsam", "yelrah"};

       

       

                      for(int i = 0; i < password.length(); i++) {

                              c = password.charAt(i);

                              for(int j = 0; j < 26; j++) {

                                      if(c == caps[j])

                                           cap++;

                              }

                      }

                      if(cap != 0)

                              score ++;

                     

                      //System.out.println(score + " caps " + cap);

                     

                      for(int i = 0; i < password.length(); i++) {

                      c = password.charAt(i);

                      for(int j = 0; j < 26; j++) {

                      if(c == lows[j]) {

                      low++;

                      }

                      }   

                      }

                  if(low != 0)

                      score ++;

                 

                  //System.out.println(score + " lows " + low);

                 

                  for(int i = 0; i < password.length(); i++) {

                  c = password.charAt(i);

                  for(int j = 0; j < 10; j++) {

                  if(c == ints[j])

                  numbers++;

                  }

                  }

                  if(numbers != 0)

                  score ++;

                     

                     // System.out.println(score + " numbers " + numbers);

                     

                      for(int i = 0; i < passwords.length; i++) {

                      String p = passwords[i];

                      if(password.equals(p))

                      score -= 5;

                      }

       

       

                      chars = (password.length() - (low + cap + numbers));

                      if(chars != 0)

                      score ++;

                     

                     // System.out.println(score + " chars " + chars);

                     

                      System.out.println();

                      System.out.println("Your score is " + score);

                      System.out.println();

                      System.out.println("Total characters: " + password.length());

                      System.out.println("CAPITAL LETTERS: " + cap);

                      System.out.println("lowercase letters: " + low);

                      System.out.println("Nu4483r5: " + numbers);

                      System.out.println("$|>3(|/-\\| ([-]/-\\|^/-\\(+3|^$: " +

                      chars);

                      input.close();

              }

      }

        • 1. Re: Help reviewing basic program for class
          448095

          Depends what you mean by "better." Does the program compile without errors or warning? Does it run without crashing. Does it produce the correct output (whatever "correct" is)? Did your instructor give any hints about what he looks for in your code?

           

          Since we don't know what the app is supposed to do (other than something to do with passwords, apparently), I don't know how anyone can suggest an improvement.

          • 2. Re: Help reviewing basic program for class
            marlin

            You are doing the same thing 3 times, testing if a character is a cap, or lower, or digit. You can do it the same way you are currently doing but you should warp it up in a function and call the function. That way your code looks more like this:

             

            if(charInSet(password.charAt(i), UPPER)){cap++;}

             

            and all your loop code is off in that function named charInSet.

             

            Furthermore, by using the indexOf function of strings you can eliminate your loop. Thus your code would look like:

             

            private final static String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            private static boolean charInSet(char c, String s){return s.indexOf("" + c) >= 0;}

             

            Helper function are ... well ... helpful.

             

            Helper classes are even better but we'll save that for later.