有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java检查输入字符串到int

我有这个方法:

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) 个答案

  1. # 1 楼答案

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

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

    问题在于函数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;
    }
    
  3. # 3 楼答案

    试试这个:

    代码:

    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