java examples

public class Test { public static void main(String[] args) { int num = 8; int num2 = 9; if(num % 2 == 0) { System.out.println(“num is an even number!”); } else { System.out.println(“num is odd number!”); } if(num2 % 2 == 0) { System.out.println(“num2 is an even number!”); } else { System.out.println(“num2 is odd number!”);…

Read More Check if the Number is Even or Odd in Java

public class Test { public static void main(String[] args) { String str = “racecar”; System.out.println(str.equals(new StringBuilder(str).reverse().toString())); } } Output: true   We can do the same with the StringBuffer class since it also has the reverse() method. Using the for loop public class Test { public static void main(String[] args) { String str = “racecar”;…

Read More Check if String is Palindrome in Java

public class Test { public static void main(String[] args) { String str = “1849231”; try { Integer.parseInt(str); Float.parseFloat(str); Double.parseDouble(str); Long.parseLong(str); System.out.println(“No error occurred. String is numeric!”); } catch (NumberFormatException e) { System.out.println(“String is not numeric!”); } } } Output: No error occurred. String is numeric!   And if we try to parse a String that…

Read More Check if String is Numeric in Java

import java.util.Arrays; public class Test { public static void main(String[] args) { int[] array = new int[]{17, 28, 9, 4, 22, 7, 1, 3, 19, 42, 58, 47}; // first sort the array Arrays.sort(array); //print the minimum value (first number in an array) System.out.println(“Minimum value: ” + array[0]); //print the maximum value (last number in…

Read More Find Max and Min Values in a Java Array

public class Test { public static void main(String[] args) { String str1 = “Learn Java Programming with alegrucoding.com”; String str2 = “Learn Java”; String str3 = “Simple Java Programming”; // check if str1 contains str2 if (str1.contains(str2)) { System.out.println(“str2 is present in str1”); } else { System.out.println(“str2 is not present in str1”); } // check…

Read More Check if a String Contains a Substring in Java

public class Test { public static void main(String[] args) { // create an object of Random class Random random = new Random(); // Generate random integer 0 to 100 int randomInteger1 = random.nextInt(101); // Generate random integer 0 to 10000 int randomInteger2 = random.nextInt(10001); // Generate random double value double randomDouble = random.nextDouble(); // Generate…

Read More Generate a Random Number in Java

public class Test { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(7, 19, 14, 1, 12, 22, 18, 3, 28, 5)); System.out.println(“Maximum value: ” + Collections.max(list)); System.out.println(“Minimum value: ” + Collections.min(list)); } } Output: Maximum value: 28 Minimum value: 1 Finding the maximum and minimum values in List using the Java 8…

Read More Find Max and Min Values of a List in Java

Output: Java   Methods from the StringUtils class that we can use to get a substring from String in Java: import org.apache.commons.lang3.StringUtils; public class Test { public static void main(String[] args) { String str = “Hello, my name is Tom, and I live in USA”; String substringBefore = StringUtils.substringBefore(str, “,”); String substringAfter = StringUtils.substringAfter(str, “,”); String substringBetween = StringUtils.substringBetween(str,…

Read More Get Substring from String in Java