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

Time:05-02

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();
            }
        }

CodePudding user response:

Within your code you're only reading and sorting the first two numbers from each file, simply because you've used the right method, nextInt(), only once per file (right before the while loop).

After that, you're trying to "iterate" the rest of the files, but instead you're only reading the rest of both files' lines with the nextLine() method not their numbers. Besides, what you're reading with the nextLine() is not even assigned to anything, discarding everything you've just read.

What you want to do is replacing those nextLine() with nextInt() and those hasNext() with hasNextInt(). Furthermore, a PrintWriter will be more helpful than just a basic FileWriter in writing the actual int value. You may find more about it in the documentation and here.

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();
            }

            //Reading the number from the second file only if this has been consumed or on the first read
            if (isRightConsumed) {
                d = sc2.nextInt();
            }

            if (c < d) {
                pw.print(String.format("%d ", c));
                isLeftConsumed = true;
                isRightConsumed = false;
            } else if (c > d) {
                pw.print(String.format("%d ", d));
                isLeftConsumed = false;
                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();
            }

            //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();
            }

            //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();
        }
    }
}
  •  Tags:  
  • java
  • Related