February 25, 2024

Java Problems Solutions: Add two numbers without using the plus operator in Java


public class AddWithoutPlus {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 5;
        int sum = add(num1, num2);
        System.out.print("Sum: ");
        System.out.println(sum);        
    }
    public static int add(int a, int b) {
        while (b != 0) {
            int carry = a & b;
            a = a ^ b;
            b = carry << 1;
        }
        return a;
    }
}

Want 1 to 1 personalized Java training? Email me at isingh30 AT gmail please. View my following video

:

February 03, 2024

Java Problems Solutions: Java Remove Unwanted Characters from String


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringCleaner {
    public static void main(String[] args) {
        String inputString = "Hello#123, Software Testing Space !Sub@scriber!";
        String cleanedString = removeUnwantedCharacters(inputString);
        System.out.println("Original String: " + inputString);
        System.out.println("Cleaned String: " + cleanedString);
    }
    // Java method to remove unwanted characters from a string
    public static String removeUnwantedCharacters(String input) {
        // Keep only alphabets and whitespace.
        // Define a regular expression.
        String regex = "[^a-zA-Z\\s]";
        // Compile the pattern and create a matcher with the input string.
        Matcher matcher = Pattern.compile(regex).matcher(input);
        // Replace unwanted characters with an empty string
        return matcher.replaceAll("");
    }
}

Want 1 to 1 personalized Java training? Email me at isingh30 AT gmail please. View my following video

:


January 29, 2024

Java Array: Largest, Smallest, Search, Sort, Find Duplicates


import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ArrayHandler {
    public static void main(String[] args) {
        int[] numbers = {5, 3, 9, 1, 5, 7, 2, 1, 8, 4, 6, 1};
        System.out.println("Find largest and smallest in an array:");
        findLargestAndSmallest(numbers);
       System.out.println("\nSearch element in an array:");
        int target = 17;
        System.out.println("Is " + target + " present? " + searchElement(numbers, target));
        System.out.println("\nSort elements in an array:");
        sortArray(numbers);
        System.out.println("\nHow to find duplicates in an array or list:");
        findDuplicates(numbers);
    }
    // Find largest and smallest in an array
    public static void findLargestAndSmallest(int[] array) {
        Arrays.sort(array);
        int smallest = array[0];
        int largest = array[array.length - 1];
        System.out.println("Smallest: " + smallest);
        System.out.println("Largest: " + largest);
    }
    // Search element in an array
    public static boolean searchElement(int[] array, int target) {
        for (int num : array) {
            if (num == target) {
                return true;
            }
        }
        return false;
    }
    // Sort elements in an array
    public static void sortArray(int[] array) {
        Arrays.sort(array);
        System.out.println("Sorted Array: " + Arrays.toString(array));
    }
    // How to find duplicates in an array or list
    public static void findDuplicates(int[] array) {
    	// A Set is a collection that does not allow duplicate elements. 
    	Set uniqueSet = new HashSet<>();
        Set duplicates = new HashSet<>();

        for (int num : array) {
            // If the element's already present, uniqueSet.add(num) returns false, meaning that it's a duplicate.
        	if (!uniqueSet.add(num)) {
                duplicates.add(num);
            }
        }
        System.out.println("Duplicate elements: " + duplicates);
    }

}

Want 1 to 1 personalized Java training? Email me at isingh30@gmail.com please.