0

So let's imagine we have this loop that obtains input from the user in the form of strings. With that input, what we want to do is set up a set of validations that will check if certain criteria are met. If all of these conditions are met, it'll complete the action in question. However; if it doesn't, it'll tell them the error and restart the process.

My question is about validating the existance (or non-existance) of a letter in a string. I have this program and for one of these validations, I need to check the entire string. If the string does not have at least one character that isn't a letter, I want to halt the action and explain that a non-letter character is required.

The problem is that I am not sure how I could replicate this in an expression in an if loop. Here's what I have so far.

public static changePassword() // Method that runs through the process of changing the password. 
 {
      // Retrieving the current and new password from the user input.
      System.out.println("Welcome to the change password screen.");
      Scanner keyboard = new Scanner(System.in);
      System.out.print("Please enter your current password: ");
      String currentPassword = keyboard.nextLine();
      System.out.print("Please enter the new password: ");
      String newPassword1 = keyboard.nextLine();
      System.out.print("Please enter the new password again: ");
      String newPassword2 = keyboard.nextLine();

      // Validating the new password entry.
      if (newPassword1.equals(newPassword2)) // Checking to see if the new password was entered exactly the same twice.
      {
        if (newPassword1.length() >= 6) // Checking to see if the new password has 6 or more characters.
        {
           if (**some expression**) // Checking to see if the password has at least one non-letter character.
           {
              currentPassword = newPassword1 // If all conditions are met, it sets the current password to the password entered by the user.
           }
           else // If there isn't a non-letter character, it informs the user and restarts the process.
           {
              System.out.println("The new password must have a non-letter character.");
              changePassword();
           }
        }
        else // If there is less than 6 characters, it informs the user and restarts the process.
        {
           System.out.println("The new password can not be less than 6 characters.");
           changePassword();
        }
      }
      else // If the new passwords don't match, it informs the user and restarts the process.
      {
        System.outprintln("The passwords must match.");
        changePassword();
      }


 }
Will Troll
  • 23
  • 1
  • 1
  • 3
  • you could use of `regex` try this [link](http://stackoverflow.com/questions/9587907/how-to-check-if-string-has-at-least-one-letter-number-and-special-character-in) – Ker p pag Sep 01 '14 at 02:18

2 Answers2

0

Assuming by "letter" you mean an english character in A-Z, a-z, just iterate through the string and return true if you encounter a character whose int value is outside the letter range.

public static boolean containsNonLetter(String s){
    for(int i = 0; i < s.length(); i++){
        int ind = (int)s.charAt(i);
        if(ind < 65 || (ind > 90 && ind < 97) || ind > 122)
            return true;
    }
    return false;
}
Mshnik
  • 7,032
  • 1
  • 25
  • 38
0

I am making the assumption that by letter you meant alphabets. If you use regex pattern you can have a very clean code as well you have ability to update the pattern as necessary. To learn more check Java Pattern. Here is the code.

private static final Pattern APLHA = Pattern.compile("\\p{Alpha}");

public static boolean hasLetter(String input) {

    return APLHA.matcher(input).find();
}
QIKHAN
  • 274
  • 2
  • 4