簡體   English   中英

僅在Java中驗證Int和雙數

[英]Validate in Java only Int and double numbers

我想實現一個驗證,它將接受來自textfield的數字(String),然后將其傳遞給double變量,如果它的Int或double,否則拋出一個新的異常。

我已經實現了一個僅適用於Int數字的代碼。 你能給我一個提示嗎?

protected double validateDiameter() {
    try {
        if (stringDiameter != null) {
     //Checking stringDiameter if its a number by creating a for loop 
            //that check each character of the string. 
            //If the string contains only numbers 
            //the program will continue, if not an exception is thrown.

            int lengthofdiameter = stringDiameter.length();

            int DiameterCount = 0;

            for (int i = 0; i <= lengthofdiameter - 1; i++) {
                char t = stringDiameter.charAt(i);
                if (Character.isDigit(t)) {
                    DiameterCount++;
                }
            }

            if (lengthofdiameter == DiameterCount) {
                Diameter = Double.parseDouble(stringDiameter);
                if (Diameter <= 0 && Diameter >= 9) {
                    throw new Exception("");
                }

            } else {
                throw new Exception("");
            }
        }
    } catch (Exception e) {
        Diameter = 0.0;

        JOptionPane.showMessageDialog(null,
                "Wrong value on the input area.Please use number." + "\n" + "Check diameter input.",
                "Error message!",
                JOptionPane.ERROR_MESSAGE);
    }

    return Diameter;
}

謝謝

更新:

謝謝大家的幫助。 我真的很感激。 對我有用的解決方案是:

protected double validateDiameter() {
    try {
        if (stringDiameter != null) {
            if (stringDiameter.matches("-?\\d+(\\.\\d+)?")) {
                Diameter = Double.parseDouble(stringDiameter);
            } else {
                throw new Exception("");
            }
            if (Diameter <= 0 || Diameter > 8) {
                throw new Exception("");
            }
        } else {
            throw new Exception("");
        }
    } catch (Exception e) {
        Diameter = 0.0;

        JOptionPane.showMessageDialog(null,
                "Wrong value on the input area.Please use number." + "\n" + 
                        "Check diameter input.",
                "Error message!",
                JOptionPane.ERROR_MESSAGE);
    }
    return Diameter;
}

我的是stringDiameter; 基本上首先你嘗試將輸入解析為Integer; 如果這不起作用你可以嘗試加倍,如果這仍然不起作用,它意味着沒有

    String s = "10.2";
    try {
        int i = Integer.parseInt(s);
        //int routine
        System.out.println(i);
    } catch (NumberFormatException e) {
        if (s.matches("-?\\d+(\\.\\d+)?")) {
            double d = Double.parseDouble(s);
            //double routine
            System.out.println(d);
        } else {
            // "Wrong value on the input area.Please use number." + "\n" + "Check                       //diameter input.", "Error message!",
            System.out.println("Wrong");
            throw new IllegalArgumentException();
        }
    }
boolean isDouble(String str) {
    try {
        Double.parseDouble(str);
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}

boolean isInteger(String s) {
    try { 
        Integer.parseInt(s); 
        return true;
    } catch(NumberFormatException e) { 
        return false; 
}

}

我必須承認我有點困惑你真正想要的東西。 上面的代碼檢查給定的字符串是否為double,是您要查找的內容?

Ben和user1121883的答案均使用異常處理。 如果需要解決方案而不使用異常(在檢查時),那么我建議您在這里查看這些答案(以及以下答案):

https://stackoverflow.com/a/5439547/1282908

https://stackoverflow.com/a/1102916/1282908

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM