I was doing my AP computer science homework and I met a strange index out of bound error:
java.lang.ArrayIndexOutOfBoundsException: 3
at Main.main(T1_L28_Template.java:68)
Here is the question:
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
Here is my code:
import java.io.*;
import static java.lang.System.*;
import java.util.Scanner;
import java.lang.Math;
class Main{
public static void main (String str[]) throws IOException {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the values for the first array, up to 10000 values, enter a negative number to quit");
int first[] = new int[10000];
int inttt=0;
int intt=0;
for(int i=0; i<first.length; i++)
{
first [i] = scan.nextInt();
inttt++;
if(first[i]<0)
{
break;
}
}
System.out.println("Enter the values for the second array, up to 10000 values, enter a negative number to quit");
int second[] = new int[10000];
for(int i=0; i<second.length; i++)
{
second [i] = scan.nextInt();
intt++;
if(second[i]<0)
{
break;
}
}
int mergeone[]=new int[inttt-1];
for(int i=0; i<mergeone.length;i++)
mergeone[i]=first[i];
int mergetwo[]=new int[intt-1];
for(int i=0; i<mergetwo.length;i++)
mergetwo[i]=second[i];
System.out.print("First Array:");
for(int i=0; i<mergeone.length; i++)
System.out.print(" "+mergeone[i]);
System.out.println(" ");
System.out.print("Second Array:");
for(int i=0; i<mergetwo.length; i++)
System.out.print(" "+mergetwo[i]);
int lengthone = mergeone.length;
int lengthtwo = mergetwo.length;
int mi;
int t=Integer.MAX_VALUE;
int mix[] = new int[lengthone+lengthtwo];
for(int i=0;i<mix.length;i++)
{
mix[i]=mergeone[i];
mix[(i+lengthone)]=mergetwo[i];
if (mix [i]< t)
t = mix[i];
}
for(int i=mix.length-1;i>=0;i--)
{
if(mergeone[i-1]>mergeone[i] || mergetwo[i-1]>mergetwo[i])
{
System.out.println("ERROR: Array not in correct order");
}
else
{
mix [0] = t;
if(mix[i]<mix[i-1])
{
mi = mix[i];
mix[i]=mix[i-1];
mix[i-1]= mi;
}
System.out.print("Merged Array:");
for(int in=0; in<mix.length;in++)
System.out.print(" "+mix[in]);
}
}
}
}