Friday, 13 April 2018

Check if string contains valid number example


  1. /*
            Check if string contains valid number example.
            This example shows how to check if string contains valid number
            or not using parseDouble and parseInteger methods of
            Double and Integer wrapper classes.
    */

    public class CheckValidNumberExample {

            public static void main(String[] args) {

                    String[] str = new String[]{“10.20”, “123456”, “12.invalid”};

                    for(int i=0 ; i < str.length ; i ++)
                    {

                            if( str[i].indexOf(“.”) > 0 )
                            {

                                    try
                                    {
                                            /*
                                             * To check if the number is valid decimal number, use
                                             * double parseDouble(String str) method of
                                             * Double wrapper class.
                                             *
                                             * This method throws NumberFormatException if the
                                             * argument string is not a valid decimal number.
                                             */
                                            Double.parseDouble(str[i]);
                                            System.out.println(str[i] + ” is a valid decimal number”);
                                    }
                                    catch(NumberFormatException nme)
                                    {
                                            System.out.println(str[i] + ” is not a valid decimal number”);
                                    }

                            }
                            else
                            {
                                    try
                                    {
                                            /*
                                             * To check if the number is valid integer number, use
                                             * int parseInt(String str) method of
                                             * Integer wrapper class.
                                             *
                                             * This method throws NumberFormatException if the
                                             * argument string is not a valid integer number.
                                             */

                                            Integer.parseInt(str[i]);
                                            System.out.println(str[i] + ” is valid integer number”);
                                    }
                                    catch(NumberFormatException nme)
                                    {
                                            System.out.println(str[i] + ” is not a valid integer number”);
                                    }
                            }
                    }

            }
    }


    /*
    Output would be
    10.20 is a valid decimal number
    123456 is valid integer number
    12.invalid is not a valid decimal number
    */

1 comment:

Codecademy Code Foundations

Popular Posts

Categories

Android (23) AngularJS (1) Assembly Language (2) Books (10) C (75) C# (12) C++ (81) Course (1) Data Strucures (4) Downloads (1) Engineering (13) flutter (1) FPL (17) Hadoop (1) HTML&CSS (40) IS (25) Java (89) Leet Code (4) Pandas (2) PHP (20) Projects (19) Python (434) R (69) Selenium Webdriver (2) Software (14) SQL (27)