Tuesday, 14 February 2017

Math : addExact




public class AddExact {

    public static void main(String[] args) {

        // Ask for user input        System.out.print("Enter 1st value:");
        // use scanner to read the console input        Scanner scan = new Scanner(System.in);
        // Assign the 1st input to String variable        String value1 = scan.nextLine();
        // ask for the second input        System.out.print("Enter 2nd value:");
        // Assign the 2nd input to String variable        String value2 = scan.nextLine();
        // close the scanner object        scan.close();
        // convert the values to long        long longVal1 = Long.parseLong(value1);        long longVal2 = Long.parseLong(value2);
        // get the result of addExact method        long result = Math.addExact(longVal1,longVal2);        System.out.print("Result of the operation:"+result);    }

}

No comments:

Post a Comment