【问题标题】:Java isLetterOrDigit() method, isDigit(), isLetter()Java isLetterOrDigit() 方法、isDigit()、isLetter()
【发布时间】:2011-12-03 20:45:27
【问题描述】:

我试图弄清楚如何检查String 以验证它是否至少包含一个字母和一个数字。我会坦率地说这是家庭作业,我有点困惑。

有一种方法isLetterOrDigit() 方法似乎是正确的方法,但我不确定如何在我的代码中实现它。这是我在下面使用的代码:

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."); 

    }
}

我想实现一个方法,其中评论部分使用isDigit()isLetter()isLetterOrDigit() 方法,但我不知道该怎么做。

任何指导将不胜感激。提前感谢您的帮助。

【问题讨论】:

    标签: java methods


    【解决方案1】:

    这应该可行。

    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 traversing through the entire string, return false
      return false;
    }
    

    【讨论】:

      【解决方案2】:

      如果不给你所有的代码来帮你做这件事是很难的,因为它太短了。

      无论如何,首先,由于您至少需要一个字母至少一个数字,因此您将需要两个标志,两个booleans,最初是false。您可以使用foreach 循环遍历ininitialPassword 中的每个char

      for (char c : initialPassword.toCharArray())
      

      然后您所要做的就是在每次迭代时检查c 是否可能是字母或数字,如果是则设置相应的标志。一旦循环终止,如果两个标志都设置了,那么你的密码是有效的。这就是您的代码的样子:

      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 */ }
      

      【讨论】:

        【解决方案3】:

        以下代码是我根据您的建议得出的最终代码:

        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's 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.");  
        
        }
        

        }

        【讨论】:

          【解决方案4】:

          这似乎是一个老问题,之前已经回答过了,但我正在添加我的代码,因为我遇到了泰语重音字符的问题。所以我努力解决这个问题,我发现上面的解决方案,如果你正在处理这样的字符,这是不完整的 - ก่อนที่สุด ท้ายo

          为了正确识别这些字符,代码如下:

          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");
              }
          

          不确定是否有人也遇到过这种情况。希望这会有所帮助。

          【讨论】:

            【解决方案5】:

            您可以使用以下代码:

            import java.util.Scanner;
            
            public class IsDigitisLetter {
            
                public static void main(String[] args) {
                    Scanner scnr = new Scanner(System.in);
            
                    char character;
                    System.out.println("Please enter a single character: ");
                    character = scnr.next().charAt(0);
            
                    System.out.println(character);
            
                    if (Character.isLetter(character)) {
                        System.out.println("The character entered is a letter.");
                    } else if (Character.isDigit(character)) {
                        System.out.println("The character entered is a digit.");
                    }
                }
            
            }
            

            【讨论】:

            • 这没有回答原来的问题。
            【解决方案6】:
            import java.util.*;
            
            public class Main {
              
                public static void main(String[] args) {
                    Scanner scanner = new Scanner(System.in);
                    String str = scanner.next();
                    boolean num = false, alpha = false;
                    for(int index = 0; index < str.length(); index++){
                      if(Character.isAlphabetic(str.charAt(index)))alpha = true;
                      else if(Character.isDigit(str.charAt(index)))num = true;
                    }
                    System.out.print(num & alpha);
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-01-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-08-02
              相关资源
              最近更新 更多