What I'm trying to do is take 2 text files that are sorted in ascending order and merge them into a third text file. The third text file must also be sorted in ascending order. Here's the problem: I cannot use arrays and sorting shouldn't be necessary.

I can merge the files fine, but I can't figure out how to get the third one to be sorted without using some sort of sorting algorithm.

Here's the code I have so far.

import java.io.*;
import java.util.Scanner;
 
public class Merge
{
	public static void main(String[] args) throws IOException
	{
		String curString1, curString2, writeString1, writeString2;
 
		Scanner fileReader1 = new Scanner(new File("list1.txt"));
		Scanner fileReader2 = new Scanner(new File("list2.txt"));
 
		FileWriter fw = new FileWriter("list3.txt");
		BufferedWriter bw = new BufferedWriter(fw);
		PrintWriter out = new PrintWriter(bw);
 
		while (fileReader1.hasNext() && fileReader2.hasNext())
		{
			curString1 = fileReader1.next();
			curString2 = fileReader2.next();
 
			if (curString1.compareTo(curString2) < 0)
			{
				out.println(curString1);
				out.println(curString2);
			}
			else if (curString1.compareTo(curString2) > 0)
			{
				out.println(curString2);
				out.println(curString1);
			}
		}
 
		if (fileReader1.hasNext())
		{
			while (fileReader1.hasNext())
			{
				out.println(fileReader1.next());
			}
		}
		else if (fileReader2.hasNext())
		{
			while (fileReader2.hasNext())
			{
				out.println(fileReader2.next());
			}
		}
		out.close();
	}
}

I realize I need to check between three strings to make sure they are in order, but I can't figure it out at all.

Thanks.