Java Method isLetterOrDigit (), isDigit (), isLetter ()

I am trying to figure out how to check a String to see if it has at least one letter and one number. I will be frank that this is homework, and I'm a little confused.

There is an isLetterOrDigit() method, which seems to be the right approach, but I don't know how to implement this in my code. Here is the code I use below:

 import javax.swing.JOptionPane; public class Password { public static void main(String[] args) { String initialPassword; String secondaryPassword; int initialLength; initialPassword = JOptionPane.showInputDialog(null, "Enter Your Passowrd."); initialLength = initialPassword.length(); JOptionPane.showMessageDialog(null, "initialLength = " + initialLength); while (initialLength < 6 || initialLength > 10) { initialPassword = JOptionPane.showInputDialog(null, "Your password does not meet the length requirements. It must be at least 6 characters long but no longer than 10."); initialLength = initialPassword.length(); } //Needs to contain at least one letter and one digit secondaryPassword = JOptionPane.showInputDialog(null, "Please enter your password again to verify."); JOptionPane.showMessageDialog(null, "Initial password : " + initialPassword + "\nSecondar Password : " + secondaryPassword); while (!secondaryPassword.equals(initialPassword)) { secondaryPassword = JOptionPane.showInputDialog(null, "Your passwords do not match. Please enter you password again."); } JOptionPane.showMessageDialog(null, "The program has successfully completed."); } } 

I want to implement a method in which the comment section uses the isDigit() , isLetter() or isLetterOrDigit() , but I just don't know how to do this.

Any guidance would be greatly appreciated. Thank you in advance.

+6
source share
4 answers

That should work.

 public boolean containsBothNumbersAndLetters(String password) { boolean digitFound = false; boolean letterFound = false; for (char ch : password.toCharArray()) { if (Character.isDigit(ch)) { digitFound = true; } if (Character.isLetter(ch)) { letterFound = true; } if (digitFound && letterFound) { // as soon as we got both a digit and a letter return true return true; } } // if not true after passing through the entire string, return false return false; } 
+5
source

It is hard to help you do this without giving you all the code to do this, since it is so short.

In any case, to begin with, since you need at least one letter and at least one number, you will need two flags, two booleans , of which initially will be false . You can iterate through char ininitialPassword using the foreach :

 for (char c : initialPassword.toCharArray()) 

And then all you have to do is check at each iteration if c is possibly a letter or number, and set the appropriate flag if that is. When the loop ends, if both checkboxes are checked, then your password is valid. Here is what your code would look like:

 boolean bHasLetter = false, bHasDigit = false; for (char c : initialPassword.toCharArray()) { if (Character.isLetter(c)) bHasLetter = true; else if (Character.isDigit(c)) bHasDigit = true; if (bHasLetter && bHasDigit) break; // no point continuing if both found } if (bHasLetter && bHasDigit) { /* valid */ } 
0
source

Below is the code I received thanks to your suggestions:

 import java.util.Scanner; public class Password { public static void main(String[] args) { String initialPassword; String secondaryPassword; int numLetterCheck = 0; int initialLength; boolean digitFound = false; boolean letterFound = false; Scanner keyboard = new Scanner(System.in); System.out.println("Enter a new password: "); initialPassword = keyboard.nextLine(); initialLength = initialPassword.length(); System.out.println("Your initial password length is: " + initialLength); while (initialLength < 6 || initialLength > 10) { System.out.println("Your password does not meet the length requirements of >6 and <10. Please enter a new password."); initialPassword = keyboard.nextLine(); initialLength = initialPassword.length(); } for (char ch : initialPassword.toCharArray()) { if (Character.isDigit(ch)) { digitFound = true; } if (Character.isLetter(ch)) { letterFound = true; } if (digitFound && letterFound) { numLetterCheck = 0; } else { numLetterCheck = 1; } } while (numLetterCheck == 1) { System.out.println("Your password must contain at least one number and one number. Please enter a new passord that meets this criteria: "); initialPassword = keyboard.nextLine(); for (char ch : initialPassword.toCharArray()) { if (Character.isDigit(ch)) { digitFound = true; } if (Character.isLetter(ch)) { letterFound = true; } if (digitFound && letterFound) { numLetterCheck = 0; } else { numLetterCheck = 1; } } } System.out.println("Please enter your password again to verify it accuracy; "); secondaryPassword = keyboard.nextLine(); System.out.println("Initial password : " + initialPassword + "\nSecondar Password : " + secondaryPassword); while (!secondaryPassword.equals(initialPassword)) { System.out.println("Your passwords do not match. Please enter your password again to verify."); secondaryPassword = keyboard.nextLine(); } System.out.println("The program has successfully completed."); } 

}

0
source

This seems to be an old question and the answer to it earlier, but I'm adding my code as I ran into a problem with Thai accented characters. So I decided to fix this problem, and I found the above solution, which was incomplete if you deal with such characters - ก ่ อน อน ี่ สุด ท้าย o

To correctly identify these characters, here is the code:

 String value = "abc123ก่อนที่สุด ท้ายo"; // Loop through characters in this String. for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); // See if the character is a letter or not. if (Character.isLetter(c)) { System.out.println("This " + c + " = LETTER"); } if (Character.isDigit(c)) { System.out.println("This " + c + " DIGIT"); } if ((""+c).matches("\\p{M}")) System.out.println("This " + c + " = UNICODE LETTER"); } 

Not sure if anyone has come across this either. Hope this helps.

0
source

Source: https://habr.com/ru/post/903014/


All Articles