我需要取两个文本文件,并按字母顺序将它们排序到一个新创建的文本文件中。
greekWriters.txt包含:
伊索
欧里庇
荷马
柏拉图
苏格拉底
romanWriters.txt包含:
西塞罗
李维
Ovid的
维吉尔
这是我的代码:
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);
}
}
这是输出:
伊索
西塞罗
欧里庇
荷马
李维
Ovid的
柏拉图
维吉尔
苏格拉底
正如你所看到的那样(Virgil和Socrates),我认为问题是在循环读取compareToIgnoreCase方法中文本文件末尾时的while循环。请帮我找一下正确排序的原因,今晚我想睡觉。先谢谢你们的帮助!
答案 0 :(得分:3)
试过这个 -
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
答案 1 :(得分:0)
我认为这是问题所在:当你退出
时
while (input1.hasNext() && input2.hasNext())
循环,s1 =&#34;柏拉图&#34;和s2 =&#34;维吉尔&#34;。然后,继续测试s1和s2并运行:
if (s1.compareToIgnoreCase(s2) <= 0)
{
output.println(s1 + "\n" + s2);
}
但问题是文件1中仍有令牌低于维吉尔(即#34;苏格拉底&#34;)。你不能假设,如果s1&lt; s2在这里你可以立即输出s2。 s1中可能有许多小于s2的名称 - 你必须在s1中循环,直到找到第一个令牌&gt; = s2。
答案 2 :(得分:0)
这是一个简短的&amp;简单的解决方案:
//read in both files
List<String> firstFileLines=Files.readAllLines(file1.toPath(),Charset. defaultCharset());
List<String> secondFileLines=Files.readAllLines(file2.toPath(),Charset. defaultCharset());
//put the lines of the two files together
ArrayList<String> allLines=new ArrayList<String>();
allLines.addAll(firstFileLines);
allLines.addAll(secondFileLines);
//sort (case insensitive & write out result
Collections.sort(allLines,String.CASE_INSENSITIVE_ORDER);
File.writeAllLines(outFile.toPath(),allLines,Charset.defaultCharset());
我提供了这个解决方案,而不是看到你的问题,因为这可能更通用(很容易扩展它来处理N文件或未分类的文件),有时一个更简单的解决方案比更快的解决方案更好。请不要冒犯,请随意忽略这个“答案”。