gpt4 book ai didi

Java 按字母顺序将两个文本文件排序为一个文本文件

转载 作者:行者123 更新时间:2023-12-01 23:28:13 25 4
gpt4 key购买 nike

我需要获取两个文本文件并将它们按字母顺序排序到一个新创建的文本文件中。

greekWriters.txt 包含:

伊索

欧里庇得斯

荷马

柏拉图

苏格拉底

romanWriters.txt 包含:

西塞罗

李维

奥维德

维吉尔

这是我的代码:

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;


public class Driver {

public static void merge(String name1, String name2, String name3)
{
File file1 = null, file2 = null, file3 = null;

Scanner input1 = null, input2 = null;

PrintWriter output = null;

try {
file1 = new File(name1);
file2 = new File(name2);
file3 = new File(name3);

input1 = new Scanner(file1);
input2 = new Scanner(file2);
output = new PrintWriter(file3);
String s1 = input1.nextLine();
String s2 = input2.nextLine();


// Problem Area
while (input1.hasNext() && input2.hasNext())
{
if(s1.compareToIgnoreCase(s2) <= 0)
{
output.println(s1);
s1 = input1.nextLine();
}

else
{
output.println(s2);
s2 = input2.nextLine();
}
}

if (s1.compareToIgnoreCase(s2) <= 0)
{
output.println(s1 + "\n" + s2);
}
else
{
output.println(s2 + "\n" + s1);
}

while (input1.hasNext())
{
output.println(input1.nextLine());
}

while (input2.hasNext())
{
output.println(input2.nextLine());
}
}

// problem area end


catch (IOException e)
{
System.out.println("Error in merge()\n" + e.getMessage());
}

finally
{
if (input1 != null)
{
input1.close();
}
if (input2 != null)
{
input2.close();
}
if (output != null)
{
output.close();
}
System.out.println("Finally block completed.");

}


}

public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
String name1, name2, name3;

name1 = "greekWriters.txt";

name2 = "romanWriters.txt";

System.out.print("Output File: ");
name3 = input.next();
merge(name1,name2,name3);
}

}

这是输出:

伊索

西塞罗

欧里庇得斯

荷马

李维

奥维德

柏拉图

维吉尔

苏格拉底

正如您所看到的,它不按顺序(Virgil 和 Socrates),我相信问题出在 while 循环上,当循环在compareToIgnoreCase 方法中读取文本文件的末尾时。请帮我找出排序不正确的原因,我今晚想 sleep 。提前感谢你们的帮助!

最佳答案

刚刚尝试过 -

<小时/>
    public static void main(String[] args) {
try {
File inputfile1 = new File("C:/_mystuff/test.txt");
File inputfile2 = new File("C:/_mystuff/test2.txt");

Scanner readerL = new Scanner(inputfile1);
Scanner readerR = new Scanner(inputfile2);

String line1 = readerL.nextLine();
String line2 = readerR.nextLine();
while (line1 != null || line2 != null) {
if (line1 == null) {
System.out.println("from file2 >> " + line2);
line2 = readLine(readerR);
} else if (line2 == null) {
System.out.println("from file1 >> " + line1);
line1 = readLine(readerL);
} else if (line1.compareToIgnoreCase(line2) <= 0) {
System.out.println("from file1 >> " + line1);
line1 = readLine(readerL);
} else {
System.out.println("from file2 >> " + line2);
line2 = readLine(readerR);
}
}
readerL.close();
readerR.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static String readLine(Scanner reader) {
if (reader.hasNextLine())
return reader.nextLine();
else
return null;
}
<小时/>

输入:

文件 1:

APPLES
CELERY
DONKEY
ZEBRA

文件 2:

BANANA
FRUIT
NINJA
ORANGE
WASHINGTON
xmas
YATCH

输出:

from file1 >> APPLES
from file2 >> BANANA
from file1 >> CELERY
from file1 >> DONKEY
from file2 >> FRUIT
from file2 >> NINJA
from file2 >> ORANGE
from file2 >> WASHINGTON
from file2 >> xmas
from file2 >> YATCH
from file1 >> ZEBRA

关于Java 按字母顺序将两个文本文件排序为一个文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19713669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com