从2个文件中读取输入,并按升序写入第三个文件

发布于 2025-01-25 21:44:26 字数 1413 浏览 3 评论 0原文

我有2个包含一系列有序数字的文件,由空格(“”)隔开。

编写一个程序,该程序产生第三个文件,该文件将包含数字上升序列。解决时,不允许您使用任何类型的集合。

文件1:1 18 40 100 文件2:0 10 15 80 1001

我设法将数字转换为string,但是在输出文件中,我只有2个第一个数字:> 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();
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲爱恨情仇 2025-02-01 21:44:26

代码的主要问题是您正在阅读和对每个文件中的前两个数字进行排序。之后,您正在阅读剩余的内容string with nextline() ,而不是使用 nextint() 读取下一个int。此外,每个erenine()读取的内容也被丢弃,因为它甚至没有分配给任何变量。

您要做的是分别替换每个nextline()hasnext()nextint() and hasnextint()< /代码>。附带说明,代码> 类,不仅是基本filewriter来写int值更有帮助。

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

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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文