w3resource

Java Project - Odd-Even numbers Sorter

Java Odd-Even Sorter: Two Solutions using Array and ArrayList:

Odd-Even Sorter:

Input: A list of numbers.
Output: Two lists: one with odd numbers, one with even numbers.

Example:

  • Input: [12, 5, 8, 7, 3]
  • Output: "Even numbers: [12, 8], Odd numbers: [5, 7, 3]"

Solution 1: Odd-Even Sorter using ArrayList

Code:

import java.util.ArrayList;
import java.util.Scanner;

public class OddEvenSorter {

    public static void main(String[] args) {
        // Initialize ArrayLists to store odd and even numbers
        ArrayList<Integer> oddNumbers = new ArrayList<>();
        ArrayList<Integer> evenNumbers = new ArrayList<>();

        // Create a scanner to read input from the user
        Scanner scanner = new Scanner(System.in);

        // Prompt the user to input the number of elements
        System.out.println("Enter the number of elements in the list: ");
        int n = scanner.nextInt();

        // Prompt the user to input the numbers
        System.out.println("Enter the numbers: ");
        for (int i = 0; i < n; i++) {
            int num = scanner.nextInt();
            if (num % 2 == 0) {
                evenNumbers.add(num);  // Add to even list if divisible by 2
            } else {
                oddNumbers.add(num);   // Add to odd list if not divisible by 2
            }
        }

        // Output the sorted lists of even and odd numbers
        System.out.println("Even numbers: " + evenNumbers);
        System.out.println("Odd numbers: " + oddNumbers);

        scanner.close();
    }
} 

Output:

Enter the number of elements in the list: 
 6
Enter the numbers: 
 12
 34
 52
 65
 63
 56
Even numbers: [12, 34, 52, 56]
Odd numbers: [65, 63]

Explanation :

  • ArrayList is used to store the odd and even numbers separately.
  • The program prompts the user for the number of inputs and the list of numbers.
  • It checks if each number is even (divisible by 2) or odd and stores them accordingly.
  • The results are printed at the end.

Solution 2: Odd-Even Sorter using Array

Code:

import java.util.Scanner;

public class OddEvenSorterArray {

    public static void main(String[] args) {
        // Create arrays for odd and even numbers with a fixed size
        int[] oddNumbers = new int[100];  // Assuming max input of 100 numbers
        int[] evenNumbers = new int[100];
        int oddIndex = 0, evenIndex = 0;

        // Create a scanner to read input from the user
        Scanner scanner = new Scanner(System.in);

        // Prompt the user for the number of elements
        System.out.println("Enter the number of elements in the list: ");
        int n = scanner.nextInt();

        // Prompt the user to input the numbers
        System.out.println("Enter the numbers: ");
        for (int i = 0; i < n; i++) {
            int num = scanner.nextInt();
            if (num % 2 == 0) {
                evenNumbers[evenIndex++] = num;  // Add to even array
            } else {
                oddNumbers[oddIndex++] = num;    // Add to odd array
            }
        }

        // Print even numbers
        System.out.print("Even numbers: ");
        for (int i = 0; i < evenIndex; i++) {
            System.out.print(evenNumbers[i] + " ");
        }
        System.out.println();

        // Print odd numbers
        System.out.print("Odd numbers: ");
        for (int i = 0; i < oddIndex; i++) {
            System.out.print(oddNumbers[i] + " ");
        }
        System.out.println();

        scanner.close();
    }
}

Output:

Enter the number of elements in the list: 
 7
Enter the numbers: 
 67
 45
 87
 890
 34
 554
 23
Even numbers: 890 34 554 
Odd numbers: 67 45 87 23

Explanation:

  • This solution uses arrays with fixed sizes to store odd and even numbers.
  • It uses oddIndex and evenIndex to track positions in the arrays.
  • Similar logic is used for sorting numbers into odd and even categories.
  • Finally, it prints the arrays with the sorted numbers.

Java Code Editor:




Follow us on Facebook and Twitter for latest update.