1

I would like to implement a validation that will accept from a textfield a number(String) and then pass it to a double variable if its Int or double, otherwise a new exception is thrown.

I already implemented a code that is working just for Int numbers. could you give me a hint please?

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;
}

Thank you

Updated:

Thanks everyone for your help. I really appreciate it. The solution that worked for me is:

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;
}
user3047017
  • 21
  • 1
  • 7

3 Answers3

2

My s is stringDiameter; Basically first you try to parse input as Integer; if that doesn't work you may try double if this still doesn't work it means is none of them

    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();
        }
    }
Liviu Stirb
  • 5,876
  • 3
  • 35
  • 40
  • Both of your answers looks to work just fine, and I can say are less complex than mine. The only problem is: A new expiation is thrown if the input is "Josh", "10.Josh" etc but inputs like "2.F" or "1.30F" are acceptable and I don't need that. How can I throw exceptions on those inputs? Thanks for your time. I really appreciate your help! – user3047017 Nov 30 '13 at 22:07
  • @user3047017 have a look at my answer http://stackoverflow.com/a/20306147/1282908 – nexus Nov 30 '13 at 22:11
  • Hey thanks for your reply, but exception handling is necessary. – user3047017 Nov 30 '13 at 22:13
  • @user3047017 probably you can use a regex to check that there are only numbers as nexus suggested – Liviu Stirb Nov 30 '13 at 22:14
  • @user3047017 have a look at those answers. There is a regex checking as well and non-exception using DURING evaluation. You can still throw an exception afterwards. – nexus Nov 30 '13 at 22:20
  • @user1121883 Thanks, That worked just fine. You saved me hours of researching.. I updated my answer with my code, if someone need it. – user3047017 Nov 30 '13 at 22:51
1
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; 
}

}

I must admit I am a bit confused what you actually want. The code above checks if the given string is a double or not, is that what you are looking for?

Ben
  • 1,157
  • 6
  • 11
  • I would like to check if the input value is an integer or double, if thats true the return that value. Otherwise a new exception is thrown. Ex: 1, 1.10, 1.30, 3 are acceptable but "text", "Josh", "123.John" are not acceptable values. Thanks for your reply – user3047017 Nov 30 '13 at 21:50
  • The code above is just the same as with integers, you just use Integer.parseInt() instead of Double. If both methods would return false, you can raise an exception - edited my post above. You are welcome. :) – Ben Nov 30 '13 at 21:53
  • I'm pretty sure it's parseable as a `Double` if it is parseable as an `Integer`, so checking both seems like overkill. – Keppil Nov 30 '13 at 22:00
  • Both of your answers looks to work just fine, and I can say are less complex than mine. The only problem is: A new expiation is thrown if the input is "Josh", "10.Josh" etc but inputs like "2.F" or "1.30F" are acceptable and I don't need that. How can I throw exceptions on those inputs? Thanks for your time. I really appreciate your help! – user3047017 Nov 30 '13 at 22:08
1

Both answers from Ben and user1121883 use exception handling. If there is a solution needed without using exceptions (while checking) then I would recommend that you have a look at those answers (and the following answers) here:

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

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

Community
  • 1
  • 1
nexus
  • 2,937
  • 3
  • 17
  • 22