-4

Please help i have been stuck for a good part of an afternoon already and cant really find/understand answers from other resources. I need to receive a password input from the user and need to test whether or not the password contains:

  • Two Upper case letters
  • Three Lower case letters
  • One number

here is my code:

import javax.swing.JOptionPane;
public class PracticePassword {

    public static void main(String[] args) {
        String originalPw;
        // TODO Auto-generated method stub
        String newPw;
        int pwLength;
        int i;
        char c;

        newPw = JOptionPane.showInputDialog(null, "Please enter a password" + 
        " that contains the following:" + "\n"
        + "**TWO upper case letters" + "\n" + "**THREE lower case letters" + "\n"
        + "**ONE number");
        originalPw = newPw;
        pwLength = newPw.length();

        for (i = 0; i <= pwLength; i++) {
            boolean hasUpperCase = !originalPw.equals(originalPw.toUpperCase());
            boolean hasLowerCase = !originalPw.equals(originalPw.toUpperCase());

            if(!hasUpperCase){
                System.out.println("You have three upper case letters, good job.");
            }
            else {
                System.out.println("You must have three upper case letters");
            }
        }

    }   
}

If it looks like i have no clue what i am doing towards the end its because i dont. Please help. thank you:)

thanks for your replies everyone. i shouldve been more specific. I guess what i shoudlve asked if there are any methods out there or structures i can implement in my code to make sure the user input meets those specifications. I see now that my code is very incomplete. i am going over notes now to improve my code.

enzo
  • 9
  • 2
  • [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/5221149) – Andreas Oct 12 '18 at 22:36
  • it looks like you've made little to no attempt to find upper/lower case letters and a number in the password. I would search for "looping a string character by character" and look up what an ASCII table is, that should help point you in the right direction for solving this – RAZ_Muh_Taz Oct 12 '18 at 22:36
  • If you need to verify if there are e.g. 3 lowercase letters, don't you think you should iterate through all the letters and **count** the number of letters that are lowercase? --- Keyword: **COUNT**. – Andreas Oct 12 '18 at 22:38
  • thanks for your replies everyone. i shouldve been more specific. I guess what i shoudlve asked if there are any methods out there or structures i can implement in my code to make sure the user input meets those specifications. – enzo Oct 12 '18 at 23:54
  • Keep in mind that complex password rules will usually not lead to more safe passwords, important is only a minimum length. People cannot remember tons of strong passwords, and such rules can interfere with good password schemes. People can get very inventive to bypass such rules, e.g. by using weak passwords like "Password-2017". Often you end up with weaker passwords instead of stronger ones. Recently NIST published an [official paper](https://pages.nist.gov/800-63-3/sp800-63b.html), advising against such rules, and against its former recommendations. – martinstoeckli Oct 13 '18 at 09:22
  • https://meta.stackoverflow.com/questions/326569/under-what-circumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest – Raedwald Dec 21 '18 at 15:00

1 Answers1

0

This looks like a homework question so I won't solve it directly. However here are some steps you could take:

  • Create a method to validate the input (public static boolean isValid(String str))

  • Convert the String to a character Array. (There's a method for that!)

  • Iterate over the letters and keep track of how many upper and lower case letters there are and how many digits there are. (Using Character.isDigit(), Character.isUpperCase() and Character.isLowerCase())

  • If all the requirements are met, return true. Return false otherwise.

GBlodgett
  • 12,704
  • 4
  • 31
  • 45