Checking each character for a number

前端 未结 6 1387
别跟我提以往
别跟我提以往 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: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

提交回复
热议问题