Checking each character for a number

前端 未结 6 1386
别跟我提以往
别跟我提以往 2021-01-20 10:12

I am trying to loop through a string and check each character if one of the characters is a number. If it is a number, I want to return it as true. I have a string \"crash\"

相关标签:
6条回答
  • 2021-01-20 10:18

    I just ran that code and I get false, as expected. Please double-check that you’re running it correctly.

    Here’s a simpler way to express that function, by the way:

    public boolean isNumber(String string) {
      for (int i = 0; i < string.length(); i++) {
        if (!Character.isDigit(string.charAt(i))) {
          return false;
        }
      }
    
      return true;
    }
    
    0 讨论(0)
  • 2021-01-20 10:18

    Your code should work correctly, although I would probably use this instead:

    public boolean isNumber(String newString)
    {
        for (int i=0; i != newString.length(); i++)
        {
            if (!Character.isDigit(newString.charAt(i))) 
            {
                return false;
            }
        }
        return true;
    }
    
    // a regex equivalent
    public boolean isNumberRegex(String newString)
    {
        return newString.match("\\d+");
    }
    

    The method above checks if all characters are digits.

    If I misunderstood your question and you want to check if any of the characters is a digit:

    public boolean hasNumber(String newString)
    {
        for (int i=0; i != newString.length(); i++)
        {
            if (Character.isDigit(newString.charAt(i))) 
            {
                return true;
            }
        }
        return false;
    }
    
    // regex equivalent
    public boolean hasNumberRegex(String newString)
    {
        return newString.match(".*\\d.*");
    }
    
    0 讨论(0)
  • 2021-01-20 10:22

    I think this code should work, but to my mind, setting a variable and then breaking just to return it is ugly. (I know other coders like this; IMHO they are wrong.) I also dislike introducing unnecessary test variables, like NullUserException's solution. I would just return directly.

    [EDIT: This code is the same as Brockman's]

    public boolean isNumber() /* Note: returns true for empty string */
    {
    String newString = "crash";
    for (int i=0; i<newString.length(); i++)
    {
       if (!Character.isDigit(newString.charAt(i)))
       {
        return false; /* non-digit detected */
       }
     }
    return true; /* all characters were digits */
    }
    
    0 讨论(0)
  • 2021-01-20 10:24

    Well you can use Integer.parseInt("string") and catch the exception.

    try {
         int num = Integer.parseInt("string");
         return true;
    } catch (NumberFormatException nfe) {
         return false;
    }
    

    Or another way with regEx:

    if ("string".replaceAll("\\d+","").length() > 0) { 
        //false 
    } else { 
        //true
    }
    
    0 讨论(0)
  • 2021-01-20 10:27

    Maybe I didn't understand you correctly... but since you're using the same variable "isNumber", and continuing when you get a positive match... the result you'll return will always be of the last character of the String, except when you get a non numeric character, in which case, you exit right away.

    Do you want to check if the whole String is a number? Or if it contains a number?

    0 讨论(0)
  • 2021-01-20 10:37
    public static boolean isNumber(String str)
    {
        int len = str.length();
        boolean isNumber = false;
    
        for(int i = 0; i < len; i++)
        {
            if(Character.isDigit(str.charAt(i)))
                return true;
        }
    
        return isNumber;
    }
    
    0 讨论(0)
提交回复
热议问题