2 methods to calculate distance between two points in java with code example. 1- Using formula. 2- Using a Point class objects. Distance between two points using formula We know that the distance between two points (x1,y1) and (x2,y2) on a flat surface is as below. That we’ll use in the java program to find […]
Extract Numbers from String – Java
Java code to extract digits or numbers from a string with logic and explanation. We’ll use 2 methods: (1) using regex expression with replaceAll method of String object, and (2) using loop and ascii values of characters. Extract Numbers from String using regex Logic: Use the regular expression [^0-9] to find non-integer characters in the […]
Sum of elements in an array in Java
Sum of elements in an array in Java with Logic and code example. Consider an integer array as an example, int[] x = { 6, 7, 2 }; //Sum of all elements of the array is 15. LOGIC: Simply retrieve each elements of the integer array by traversing the array using a for loop till […]