In this lab, you will be creating a program that merges two arrays of non-negative (equal to or greater than 0) integers. Your program will accept each array as input from the keyboard. You do not know ahead of time how many values will be entered, but you can assume each array will have a maximum length of 10,000 elements. To stop entering values enter a negative number. You may disregard any negative numbers input and not store these in the array. The elements of the two input arrays should be in increasing order. In other words, each array element must have a value that is greater than or equal to the previous element value. An array may contain repeated elements. After the two arrays have been input, your program must check to make sure the elements of each array have been entered in order. If an out of order element is found, print the message “ERROR: Array not in correct order”. Your task is to merge the two input arrays into a new array, with all elements in order, lowest to highest. Print out each of the original arrays entered, followed by the merged array. Please note that your program must output the arrays with exactly one space between each of the numbers. Sample Run 1: Enter the values for the first array, up to 10000 values, enter a negative number to quit
3 3 5 6 8 9 -1
Enter the values for the second array, up to 10000 values, enter a negative number to quit
3 4 5 6 -5
First Array:
3 3 5 6 8 9
Second Array:
3 4 5 6
Merged Array:
3 3 3 4 5 5 6 6 8 9
Sample Run 2: Enter the values for the first array, up to 10000 values, enter a negative number to quit
4 5 7 2 -1
Enter the values for the second array, up to 10000 values, enter a negative number to quit
3 3 3 3 3 3 -100
First Array:
4 5 7 2
Second Array:
3 3 3 3 3 3
ERROR: Array not in correct order
import java.io.*;
import static java.lang.System.*;
import java.util.Scanner;
import java.lang.Math;
import java.lang.Object;
import java.util.Arrays;
import java.util.ArrayList;
import org.apache.commons.lang3.ArrayUtils;
class Main
{
public static void main (String str[]) throws IOException {
{
Scanner scan = new Scanner(System.in);
int[] arrayone = new int[10000];
int[] arraytwo = new int[10000];
int [] mergeQ = new int[arrayone.length + arraytwo.length];
int integers = 0;
int inte = 0;
System.out.println("\nEnter the values for the first array, up to 10000 values, enter a negative number to quit");
for (int i = 0; i < arrayone.length; i++)
{
arrayone[i] = scan.nextInt();
if (arrayone[i] < 0){
break;
} else {integers ++;}
}
System.out.println("\nEnter the values for the second array, up to 10000 values, enter a negative number to quit");
for (int i=0; i<arraytwo.length; i++)
{
arraytwo[i] = scan.nextInt();
if (arraytwo[i] < 0)
{
break;
} {inte ++;}
}
System.out.println("First Array:");
for (int i=0; i< integers; i++)
{
System.out.print(arrayone[i] + " ");
}
System.out.println("\nSecond Array:");
for (int i=0; i< inte; i++)
{
System.out.print(arraytwo[i] + " ");
}
System.out.println("\nMerged Array:");{
String[] both = ArrayUtils.addAll(arrayone[integer], arraytwo[inte]);
Arrays.sort(both);
}
}
}
}
you were adding elements but not the arrays themselves.
In you second loop
}{ inte++; }
change this to
}else {inte ++;}
one solution could be
int[] both = ArrayUtils.addAll(Arrays.copyOf(arrayone, integers), Arrays.copyOf(arraytwo,inte));
Arrays.sort(both);
for (int i=0; i< both.length; i++){
System.out.print(both[i] + " ");
A very bad way to do it.. but will work in your CS class
for (int i=0; i< integers; i++){
mergeQ[i] = arrayone[i];
}
for (int i=integers; i< inte + integers; i++){
mergeQ[i] = arraytwo[i - integers];
}
int both[] = Arrays.copyOf(mergeQ,integers+inte);
Arrays.sort(both);
for (int i=0; i< both.length; i++){
System.out.print(both[i] + " ");
}
Here is another way it is a bit cleaner but roughly has the same performance as the one above..
System.arraycopy(arrayone, 0, mergeQ, 0, integers);
System.arraycopy(arraytwo, 0, mergeQ, integers, inte);
int both[] = Arrays.copyOf(mergeQ,integers+inte);
Arrays.sort(both);
for (int i=0; i< both.length; i++){
System.out.print(both[i] + " ");
}