• 0

[JAVA] Merge Sorted Files


Question

Hey,

I'm having an issue with trying to figure out how to take two text files that contain strings (sorted in ascending order), and merge them into a third text file that is also sorted in ascending order. The problem is I can't use arrays and I shouldn't have to sort the strings. Also, the two text files are not necessarily the same length.

I've tried several times, but I can't seem to figure it out. Mainly, what I can't figure out is how to simultaneously read the two files together and then compare each line as they come in. I've tried using compareTo which works, but I have it set up in a while loop which is reading both files at the same time (ie. while (fileReader1.hasNext() && fileReader2.hasNext()).

Anyone have any ideas?

Link to comment
https://www.neowin.net/forum/topic/874284-java-merge-sorted-files/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Here's what I have so far, it sorts most of it all right but obviously it stops once one of the files reaches the end and won't continue.

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

public class Merger
{
	public static void main(String[] args) throws IOException
	{
		String file1 = "list1.txt";
		String file2 = "list2.txt";
		String outFile = "list3.txt";
		String curString1, curString2, curString3, writeString1, writeString2, writeString3;
		int count1, count2, max;

		Scanner fileReader1 = new Scanner(new File(file1));
		Scanner fileReader2 = new Scanner(new File(file2));

		FileWriter fw = new FileWriter(outFile);
		BufferedWriter bw = new BufferedWriter(fw);
		PrintWriter out = new PrintWriter(bw);

		while (fileReader1.hasNext() && fileReader2.hasNext())
		{
			curString1 = fileReader1.next();
			curString2 = fileReader2.next();

			if (fileReader1.hasNext())
			{
				curString3 = fileReader1.next();
			}
			else
			{
				curString3 = fileReader2.next();
			}


			if (curString1.compareTo(curString2) < 0)
			{
				if (curString2.compareTo(curString3) < 0)
				{
					writeString1 = curString1;
					writeString2 = curString2;
					writeString3 = curString3;

					out.print(writeString1);
					out.println();
					out.print(writeString2);
					out.println();
					out.print(writeString3);
					out.println();
				}
				else if (curString2.compareTo(curString3) > 0)
				{
					writeString1 = curString1;
					writeString2 = curString3;
					writeString3 = curString2;

					out.print(writeString1);
					out.println();
					out.print(writeString2);
					out.println();
					out.print(writeString3);
					out.println();
				}
			}
			else if (curString2.compareTo(curString1) < 0)
			{
				if (curString1.compareTo(curString3) < 0)
				{
					writeString1 = curString2;
					writeString2 = curString1;
					writeString3 = curString3;

					out.print(writeString1);
					out.println();
					out.print(writeString2);
					out.println();
					out.print(writeString3);
					out.println();
				}
				else if (curString1.compareTo(curString3) > 0)
				{
					writeString1 = curString2;
					writeString2 = curString3;
					writeString3 = curString1;

					out.print(writeString1);
					out.println();
					out.print(writeString2);
					out.println();
					out.print(writeString3);
					out.println();
				}
			}
		}
		out.close();
	}
}

  • 0

Well the loop condition is wrong, you want to keep adding entries while one OR the other file has remaining entries, not AND. Basically the algorithm is:

while file1 or file2 has a remaining entry:
	if only one has an entry:
		add it to the new file
	if both have entries:
		take the smallest 
		add it to the new file

Of course you need to maintain some kind of counter for each file indicating what's the next entry and increment it when you take an entry.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.