javajava.util.scannerjava-io

Reading input from 2 files and writing to a third file in ascending order


I have 2 files that each contain a series of ordered numbers, separated by spaces (" ").

Write a program that produces a third file that will contain the ascending sequence of numbers. When solving, you are not allowed to use any type of collection.

File 1: 1 18 40 100 File 2: 0 10 15 80 1001

I managed to convert the number to String, but in the output file I've got the only 2 first numbers sorted : 0 1

FileWriter outputFile;
Scanner sc1 = null;
Scanner sc2 = null;
try {
    sc1 = new Scanner(new FileReader("Numbers1.txt"));
    sc2 = new Scanner(new FileReader("Numbers2.txt"));
    outputFile = new FileWriter("NumbersMerge.txt");
    int c = sc1.nextInt();
    int d = sc2.nextInt();
    while (sc1.hasNext() && sc2.hasNext()) {
        if (c < d) {
            outputFile.write(Integer.toString(c));
            sc1.nextLine();
        } else if (c > d) {
            outputFile.write(Integer.toString(d));
            sc2.nextLine();
        } else {
            outputFile.write(Integer.toString(c));
            outputFile.write(Integer.toString(d));
            sc1.nextLine();
            sc2.nextLine();
        }
    }
     if (sc1.hasNext()) {
        outputFile.write(Integer.toString(c));
        sc1.nextLine();
    }
    if (sc2.hasNext()) {
        outputFile.write(Integer.toString(d));
        sc2.nextLine();
    }
    outputFile.flush();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (sc1 != null && sc2 != null) {
        sc1.close();
        sc2.close();
    }
}

Solution

  • The main problem with your code is that you're reading and sorting only the first two numbers from each file. After that, you're reading the remaining content a String with nextLine(), instead of using nextInt() to read the next int. Furthermore, the content read by each reaLine() is also discarded, as it's not even assigned to any variable.

    What you want to do is to respectively replace each nextLine() and hasNext() with nextInt() and hasNextInt(). As a side note, the PrintWriter class would be more helpful than just a basic FileWriter to write int values.

    public static void main(String[] args) {
        Scanner sc1 = null;
        Scanner sc2 = null;
        PrintWriter pw = null;
        int c = 0, d = 0;
        boolean isLeftConsumed = true;
        boolean isRightConsumed = true;
        try {
            sc1 = new Scanner(new FileReader("Numbers1.txt"));
            sc2 = new Scanner(new FileReader("Numbers2.txt"));
            pw = new PrintWriter(new FileWriter("NumbersMerge.txt"));
    
            //Keep reading as long as both files have numbers left
            while (sc1.hasNextInt() && sc2.hasNextInt()) {
    
                //Reading the number from the first file only if this has been consumed or on the first read
                if (isLeftConsumed) {
                    c = sc1.nextInt();
                    isLeftConsumed = false;
                }
    
                //Reading the number from the second file only if this has been consumed or on the first read
                if (isRightConsumed) {
                    d = sc2.nextInt();
                    isRightConsumed = false;
                }
    
                if (c < d) {
                    pw.print(String.format("%d ", c));
                    isLeftConsumed = true;
                } else if (c > d) {
                    pw.print(String.format("%d ", d));
                    isRightConsumed = true;
                } else {
                    pw.print(String.format("%d ", c));
                    pw.print(String.format("%d ", d));
                    isLeftConsumed = true;
                    isRightConsumed = true;
                }
            }
    
            //Writing the remaining numbers from the first file
            while (sc1.hasNextInt()) {
    
                //Reading the number from the first file only if this has been consumed or on the first read
                if (isLeftConsumed) {
                    c = sc1.nextInt();
                    isLeftConsumed = false;
                }
    
                //If the last number from the second file hasn't been written yet, then we keep checking whether it can be added or not
                if (!isRightConsumed) {
                    if (c < d) {
                        pw.print(String.format("%d ", c));
                        isLeftConsumed = true;
                    } else if (c > d) {
                        pw.print(String.format("%d ", d));
                        isRightConsumed = true;
                    } else {
                        pw.print(String.format("%d ", c));
                        pw.print(String.format("%d ", d));
                        isLeftConsumed = true;
                        isRightConsumed = true;
                    }
                } else {
                    //Case where the last number from the second file has been written and there are still numbers left from the first file
                    pw.print(String.format("%d ", c));
                    isLeftConsumed = true;
                }
            }
    
            //Writing the remaining numbers from the second file
            while (sc2.hasNext()) {
    
                //Reading the number from the second file only if this has been consumed or on the first read
                if (isRightConsumed) {
                    d = sc2.nextInt();
                    isRightConsumed = false;
                }
    
                //If the last number from the first file hasn't been written yet, then we keep checking whether it can be added or not
                if (!isLeftConsumed) {
                    if (c < d) {
                        pw.print(String.format("%d ", c));
                        isLeftConsumed = true;
                    } else if (c > d) {
                        pw.print(String.format("%d ", d));
                        isRightConsumed = true;
                    } else {
                        pw.print(String.format("%d ", c));
                        pw.print(String.format("%d ", d));
                        isLeftConsumed = true;
                        isRightConsumed = true;
                    }
                } else {
                    //Case where the last number from the first file has been written and there are still numbers left from the second file
                    pw.print(String.format("%d ", d));
                    isRightConsumed = true;
                }
            }
    
            //Checking whether the last number from the first file hasn't been written yet (case of the greatest of all)
            if (!isLeftConsumed) {
                pw.print(String.format("%d ", c));
            }
    
            //Checking whether the last number from the second file hasn't been written yet (case of the greatest of all)
            if (!isRightConsumed) {
                pw.print(String.format("%d ", d));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (sc1 != null) {
                sc1.close();
            }
            if (sc2 != null) {
                sc2.close();
            }
            if (pw != null) {
                pw.close();
            }
        }
    }