2013-10-31 28 views
0

我需要將兩個文本文件按字母順序排列到一個新創建的文本文件中。Java將兩個文本文件按字母順序排列到一個文本文件中

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

} 

這是輸出:

伊索

西塞羅

歐裏庇

荷馬

李維

Ovid的

柏拉圖

維吉爾

蘇格拉底

正如你可以看到它是不是爲了(維吉爾和蘇格拉底),我相信這個問題是當環路閱讀文本文件到底while循環compareToIgnoreCase方法。請幫我找出它沒有正確排序的原因,我今晚想睡覺。謝謝你們提前給予的幫助!

+0

將輸入文件始終進行排序?他們可能是任何規模,或者他們會一直很小? – DNA

+0

問題可能在於這條線,如果(s1.compareToIgnoreCase(s2)<= 0)只是通過查看它。我會運行一些測試。 – Dom

+0

它們應該按任意大小排序,我只是爲了舉例的目的 – user2673161

回答

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 
0

我覺得這裏的問題:當您退出

while (input1.hasNext() && input2.hasNext())

循環,S1 = 「柏拉圖」 和s2 = 「維吉爾」。然後你繼續前進的S1和S2的測試和運行:

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

但問題是,仍然有令牌文件1是小於維吉爾(即「蘇格拉底」)。你不能假設,如果s1 < s2在這裏,你可以立即輸出s2。s1中可能有很多名稱小於s2的名稱 - 必須在s1中循環,直到找到第一個令牌> = s2。

0

下面是一個簡短&簡單的解決方案:

//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-文件或者未分類文件),有時候更簡單的解決方案比更快的解決方案更好。請不要冒犯,請隨意忽略這個「答案」。

相關問題