检查输入字符串为int

时间:2016-12-10 19:01:45

标签: java string

我有这个方法:

public static int parseInt(String str) {
    if (isValidNumber(str)) {
        int sum = 0;
        int position = 1;
        for (int i = str.length() - 1; i >= 0; i--) {
            int number = str.charAt(i) - '0';
            sum += number * position;
            position = position * 10;
        }
        return sum;
    }
    return -1;
}

将字符串转换为整数。正如你所看到的那样(目前)在一个if语句中,一个方法检查输入是否是我的目的的有效输入:

public static boolean isValidNumber(String str) {
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if(c >= '0' && c <= '9'){
            return true;
        }
    }
    return false;
}

我希望字符串只是数字(负数和正数),不允许其他数字。那时,一个字符串,即1a1a将被转换为一个不应该转换的整数,而-1将不会被转换。我想你们明白我的意思。我不知道该怎么做。

请帮忙!

3 个答案:

答案 0 :(得分:1)

试试这个:

<强> CODE:

public class validNumbers {

    public static void main(String[] args) {

        System.out.println(parseInt("345"));
        System.out.println(parseInt("-345"));
        System.out.println(parseInt("a-345"));
        System.out.println(parseInt("1a5b"));
    }

    public static int parseInt(String str) {
        String numberWithoutSign = removeSign(str);
        if (isValidNumber(numberWithoutSign)) {
            int sum = 0;
            int position = 1;
            for (int i = numberWithoutSign.length() - 1; i >= 0; i--) {
                int number = numberWithoutSign.charAt(i) - '0';
                sum += number * position;
                position = position * 10;
            }
            if(isNegative(str)){
                return -(sum);
            }else{
                return sum;
            }
        }
        return -1;
    }

    /**
     * Removes sign in number if exists
     */
    public static String removeSign(String number){
        if(number.charAt(0) == '+' || number.charAt(0) == '-'){
            return number.substring(1);
        }else{
            return number;
        }
    }
    /**
     * Determines if a number is valid
     */
    public static boolean isValidNumber(String number) {
        for (int i = 0; i < number.length(); i++) {
            char c = number.charAt(i);
            if(c >= '0' && c <= '9'){
                continue;
            }else{
                return false;
            }
        }
        return true;
    }

    /**
     * Determines if a number is negative or not
     */
    public static boolean isNegative(String number){
        if(number.charAt(0) == '-'){
            return true;
        }else{
            return false;
        }
    }

}

<强>输出:

345
-345
-1
-1

答案 1 :(得分:0)

要检查字符串是否为实数,您可以使用以下方法:

    public static boolean isInteger(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException nfe) {}
        return false;
    }

答案 2 :(得分:0)

问题在于您的功能isValidNumber。它应该在第一次出现非数字值时返回false,如下所示:

public static boolean isValidNumber(String str) {
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if(!(c >= '0' && c <= '9')){
            if (i > 0) {
                return false;
            }

            //This will only be invoked when `i == 0` (or less, which is impossible in this for loop), so I don't need to explicitly specify it here, as I have checked for `i > 0` in the above code...    
            if (c != '-' && c != '+') {
                return false;
            }
        }
    }

    return true;
}
相关问题