java examples

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

public class Test { public static void main(String[] args) { String stringToReverse = “Hello World of Java!”; StringBuilder stringBuilder = new StringBuilder(stringToReverse); String result = stringBuilder.reverse().toString(); System.out.println(result); } } Output: !avaJ fo dlroW olleH   We can do the same using the StringBuffer class since it also has the reverse() method. Reverse a String using the…

Read More Reverse a String in Java

public class Test { public static void main(String[] args) { String str = “Learn Java with alegrucoding.com”; // extract the substring starting from index 6 String substring = str.substring(6); // extract the substring from index 0 (inclusive) to 10 (exclusive) String substring2 = str.substring(0, 10); System.out.println(substring); System.out.println(substring2); } } Output: Java with alegrucoding.com Learn Java…

Read More Get Substring from String in Java