Java Program to Put Positive and Negative Numbers in Separate Array

Write a Java Program to Put Positive and Negative Numbers in Separate Array with an example. Or how to write a Java Program to find and place Positive and negative items in separate arrays.

In this Java Positive and Negative Numbers in Separate Array example, we used the while loop to iterate count_PosNegArr array. We used the If Else statement to store positive items in pos_Arr and negative items in neg_Arr within the Loop. Later, we printed the same.

package ArrayPrograms;

public class SeparatePositiveNegativeArrayItems0 {
	
	public static void main(String[] args) {
		
		int i = 0, pos_count = 0, neg_count = 0;
		int[] count_PosNegArr = {15, -4, 11, -8, -13, 2, 16, -11, 9, -5, 18, 60};
		int[] pos_Arr = new int[10];
		int[] neg_Arr = new int[10];
		
		while(i < count_PosNegArr.length) 
		{
			if(count_PosNegArr[i] >= 0) {
				pos_Arr[pos_count] = count_PosNegArr[i];
				pos_count++;
			}
			else {
				neg_Arr[neg_count] = count_PosNegArr[i];
				neg_count++;
			}
			i++;
		}
		System.out.println("\nThe Total Number of Positive Items = " + pos_count);
		System.out.print("The List of Items in Positive Array = ");
		PrintArrayElement(pos_Arr, pos_count);
		
		System.out.println("\n\nThe Total Number of Negative Items = " + neg_count);
		System.out.print("The List of Items in Negative Array = ");
		PrintArrayElement(neg_Arr, neg_count);
	}
	public static void PrintArrayElement(int[] arr, int size) {
		int i;
		
		for(i = 0; i < size; i++) 
		{
			System.out.format("%d ", arr[i]);
		}
	}
}
The Total Number of Positive Items = 7
The List of Items in Positive Array = 15 11 2 16 9 18 60 

The Total Number of Negative Items = 5
The List of Items in Negative Array = -4 -8 -13 -11 -5 

Java Program to Put Positive and Negative Numbers in Separate Array using For Loop

In this Java example, we replaced the while loop with for loop, and it also allows the user to enter the count_PosNegArr array size and items.

package ArrayPrograms;

import java.util.Scanner;

public class SeparatePositiveNegativeArrayItems1 {
	private static Scanner sc;
	public static void main(String[] args) {
		
		int i, Size, pos_count = 0, neg_count = 0;
		
		sc = new Scanner(System.in);
		
		System.out.print("\nPlease Enter the PUT POS & NEG Array size  : ");
		Size = sc.nextInt();
		
		int[] pos_Arr = new int[Size];
		int[] neg_Arr = new int[Size];
		int[] count_PosNegArr = new int[Size];
		
		System.out.format("\nEnter PUT POS & NEG Array %d elements : ", Size);
		for(i = 0; i < Size; i++) 
		{
			count_PosNegArr[i] = sc.nextInt();
		}
		
		for(i = 0; i < count_PosNegArr.length; i++) 
		{
			if(count_PosNegArr[i] >= 0) {
				pos_Arr[pos_count] = count_PosNegArr[i];
				pos_count++;
			}
			else {
				neg_Arr[neg_count] = count_PosNegArr[i];
				neg_count++;
			}
		}
		System.out.println("\nThe Total Number of Positive Items = " + pos_count);
		System.out.print("The List of Items in Positive Array = ");
		PrintArrayElement(pos_Arr, pos_count);
		
		System.out.println("\n\nThe Total Number of Negative Items = " + neg_count);
		System.out.print("The List of Items in Negative Array = ");
		PrintArrayElement(neg_Arr, neg_count);
	}
	public static void PrintArrayElement(int[] arr, int size) {
		int i;
		
		for(i = 0; i < size; i++) 
		{
			System.out.format("%d ", arr[i]);
		}
	}
}
Java Program to Put Positive and Negative Numbers in Separate Array 2

In this Java Positive Negative Numbers in Separate Array example, we created a separate void function, PutPositiveItems, to put positive items into pos_Arr. And another function PutNegativeItems to put the negative items into neg_arr.

package ArrayPrograms;

import java.util.Scanner;

public class SeparatePositiveNegativeArrayItem2 {
	private static Scanner sc;
	public static void main(String[] args) {
		
		int i, Size;
		
		sc = new Scanner(System.in);
		
		System.out.print("\nPlease Enter the PUT POS & NEG Array size  : ");
		Size = sc.nextInt();
		
		int[] count_PosNegArr = new int[Size];
		
		System.out.format("\nEnter PUT POS & NEG Array %d elements : ", Size);
		for(i = 0; i < Size; i++) 
		{
			count_PosNegArr[i] = sc.nextInt();
		}
		PutPositiveItems(count_PosNegArr,Size);
		PutNegativeItems(count_PosNegArr, Size );

	}
	public static void PutPositiveItems(int[] count_PosNegArr, int size ) {
		int i, pos_count = 0;
		int[] pos_Arr = new int[size];

		for(i = 0; i < count_PosNegArr.length; i++) 
		{
			if(count_PosNegArr[i] >= 0) {
				pos_Arr[pos_count] = count_PosNegArr[i];
				pos_count++;
			}
		}
		System.out.println("\nThe Total Number of Positive Items = " + pos_count);
		System.out.print("The List of Items in Positive Array = ");
		PrintArrayElement(pos_Arr, pos_count);
	}
	public static void PutNegativeItems(int[] count_PosNegArr, int size ) {
		int i, neg_count = 0;
		int[] neg_Arr = new int[size];
		
		for(i = 0; i < count_PosNegArr.length; i++) 
		{
			if(count_PosNegArr[i] < 0) {

				neg_Arr[neg_count] = count_PosNegArr[i];
				neg_count++;
			}
		}
		System.out.println("\n\nThe Total Number of Negative Items = " + neg_count);
		System.out.print("The List of Items in Negative Array = ");
		PrintArrayElement(neg_Arr, neg_count);
	}
	public static void PrintArrayElement(int[] arr, int size) {
		int i;
		
		for(i = 0; i < size; i++) 
		{
			System.out.format("%d ", arr[i]);
		}
	}
}
Please Enter the PUT POS & NEG Array size  : 11

Enter PUT POS & NEG Array 11 elements : 19 -33 -55 29 -66 -77 39 59 -88 -99 79

The Total Number of Positive Items = 5
The List of Items in Positive Array = 19 29 39 59 79 

The Total Number of Negative Items = 6
The List of Items in Negative Array = -33 -55 -66 -77 -88 -99