Checking each character for a number

前端 未结 6 1390
别跟我提以往
别跟我提以往 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;
    }
    

提交回复
热议问题