user1860986
user1860986

Reputation: 1

Merging Two Arrays Given In a Text File into a Third Array

Yes this is homework. Simply put because my professor isn't the best at wording,

I am given a text file as such

11 -5 -4 -3 -2 -1 6 7 8 9 10 11
8 -33 -22 -11 44 55 66 77 88

This is given as SortedArrays.txt

The first row would be Array 1 and the second row would be Array 2, the first int of each line would be the size of array so:

int a[] = new int[11]
int b[] = new int[8]

My first issue is reading this into the program, I'm assuming run as an argument, but my issue is deciphering between each line and loading the array.

I believe I have a good method for comparing the a and b elements and loading into c, but can anyone take a look over it?

Thanks again for this site, I've searched around and have seen this program but the elements were already given or not given from a text file.

// ************************************************************ 
// MergeArray.java 
// 
// Written by: Brandon Pham
//
// 
// 
//
// Homework 6
// ************************************************************


import java.io.*;
import java.util.*;

public class MergeArray { 
String arrayfile = "SortedArrays.txt"; 
    MergeArray(String arrayfile){
    try { 
     Scanner arrayload = new Scanner(File(arrayfile)); 
    }  
  catch (FileNotFoundException e) { 
     System.out.printf("File %s not found, exiting!",  
                       arrayfile); 
     System.exit(0); 
  } 
int[] a = new int[arrayload.nextint()];
int[] b = new int[arrayload.nextLine()];
int[] c = new int[a.length+b.length];
}
public static void merge(int[]a, int[]b, int[]c) {
int i=0,j=0,k=0;
 int alength = a.length;
 int blength = b.length;
while (k<c.length){
  if (a[i] < b[j]){
    c[k] = a[i];
      k++;
      i++;
        }
    if (a[i] == b[j]){
        c[k]=a[i];
        c[k++]=b[j];
        i++;
        j++;
    if (i+1>a.length){
        c[k]=b[j];
        k++;
        j++;
        }
    if (j+1>b.length){
        c[k]=a[i];
        k++;
        i++;
        }
  else{
    c[k] = b[j];
      k++;
      j++;
    }        
  }
}

Upvotes: 0

Views: 823

Answers (2)

Miquel
Miquel

Reputation: 15675

Your file reading's good. The sorting of the output however, could be a bit simpler:

Here's some quick code:

int[] result = new int[array1.length+array2.length]
int cursor1 = 0;
int cursor2 = 0;
for(int = 0; i < result.length; i++) {
    if(cursor1 > array1.length-1){
         result[i] = array2[cursor2++];
    } else if(cursor2 > array2.length-1){
         result[i] = array1[cursor1++];
    } else if(array1[cursor1] > array2[cursor2]){
        result[i] = array1[cursor1];
        cursor1++;
    } else {
        result[i] = array2[cursor2];
        cursor2++;
    }
}

I just coded this in the browser, so I'm certainly not vouching for correct syntax ;)

Hope this helps, please ask again for further details, and feel free to post your progress.

Upvotes: 0

Yogendra Singh
Yogendra Singh

Reputation: 34367

Try this : read one line at a time, split the line using space to get array of numbers in the line, add all entries to a list as it has open end. Finally get the array from the list.

   String line;
   List<String> list = new ArrayList<String>();
   while(arrayload.hasNextLine()){
      line = arrayload.nextLine();
      String[] nums = line.split(" ");
      list.addAll(Arrays.asList(nums));
   }
   String[] finalNums = list.toArray(new String[]{});

Upvotes: 1

Related Questions