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

You could just combine the two arrays into a new array and then sort it. When you are going through both of you two orginal arrays, some kind of sorting comparison must be done to combine the two into the new sorted order. I don't know if this helps, but I would keep it simple.

Cool thanks for your feedback, I'll look into it.

You are NOT using StringTokenizer to merge the 2 arrays into the 3rd. You are using it to read the 2 arrays from a txt file. In that context, your strategy will depend on the way the data is distributed in that txt file.

As far as the merging is concerned, I believe you're looking into a better solution ^^

also: before making the choice to use StringTokenizer, read this line from the StringTokenizer api:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

Okay great, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.