Hi all, hope you can give me some help to edit my code. I'm unaware how to merge two arrays and allocate them to a third array(of the right size) and let's just say for the sake of things, we'll call it C. I went ahead and instead used the StringTokenizer, but I assume there's a simpler way to put it together.
(Also, I'm getting the data from a .txt file, for my instance the file is called "SortedArrays.txt")
import java.io.*;
import java.util.*;
class SortedArrays
{
public static void main(String args[])throws FileNotFoundException{
File f = new File("SortedArrays.txt");
Scanner s =new Scanner(f);
StringTokenizer h= new StringTokenizer(s.nextLine());
int b=Integer.parseInt(h.nextToken());
int[] ar1 = new int [b];
int i;
i=0;
while(h.hasMoreTokens() && i<b){
ar1[i]= Integer.parseInt(h.nextToken());
i++;
}
h= new StringTokenizer(s.nextLine());
int bb=Integer.parseInt(h.nextToken());
int[] ar2 = new int [bb];
i=0;
while(h.hasMoreTokens() && i<bb){
ar2[i]= Integer.parseInt(h.nextToken());
i++;
}
s.close();
i=0;
System.out.print("ArrayA: ");
while(i<ar1.length){
System.out.print(ar1[i]+" ");
i++;
}
i=0;
System.out.print("\n");
System.out.print("ArrayB: ");
while(i<ar2.length){
System.out.print(ar2[i]+" ");
i++;
}
int[] ar3 = new int[ar1.length + ar2.length];
int index = 0;
for(i = 0;i < ar1.length;i++){
ar3[index] = ar1[i];
index++;
}
for(i = 0;i < ar2.length;i++){
ar3[index] = ar2[i];
index++;
}
Arrays.sort(ar3);
i=0;
System.out.print("\n");
System.out.print("ArrayC: ");
while(i<ar3.length){
System.out.print(ar3[i]+" ");
i++;
}
}
}
Basically, how can I re-edit/write this code without using StringTokenizer to allocate two arrays into a third? If anyone could show me how, it'd be extremely helpful. I tried my best to do it alone, but just reverted to using StringTokenizer. **yes this code is mine