2016-06-13 15 views
1

2つの異なるテキストファイルを3番目のファイルにマージして昇順に並べ替えるプログラムを作成しようとしています。 12 23 34 45 56 67 69 123 1332つのテキストファイルをマージして3番目のファイルにソート

ファイル4:2:5 10 20 35 44 100 130 150 160 180

そしてここされ、ここで2つのファイルがその中に

ファイル1を持っているものです私がこれまで持っているコード:

try 
    { 

     FileReader file1=new FileReader("D://School//text1.txt"); 
     Scanner scan = new Scanner(new File("D://School//text1.txt")); 
     ArrayList<Integer> values = new ArrayList<Integer>(); 
     Collections.sort(values);  //sorting the values 
     while(scan.hasNextInt()) values.add(scan.nextInt()); 

     FileReader file2=new FileReader("D://School//text2.txt"); 
     scan = new Scanner(new File("D://School//text2.txt")); 
     values = new ArrayList<>(); 
     Collections.sort(values);  //sorting the values. 
     while(scan.hasNextInt()) values.add(scan.nextInt()); 

     BufferedReader br1 = new BufferedReader (file1); 
     BufferedReader br2 = new BufferedReader(file2); 

     String temp1 = ""; 
     String temp2 = ""; 

     while(br1.readLine() !=null) 
     { 
      temp1=br1.readLine()+temp1; 
     } 
     while(br2.readLine()!=null) 
     { 
      temp2=br2.readLine()+temp2; 
     } 
     String temp = temp1 + temp2; 



     FileWriter fw=new FileWriter("D://School//text3.txt"); 
     char buffer[]=new char[temp.length()]; 
     temp.getChars(0,temp.length(),buffer,0);       
     fw.write(buffer); 
     file1.close(); 
     file2.close(); 
     fw.close(); 
    } 

    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 

プログラムは、これまでのところしかし、それは昇順にそれをソートされていない、第三のファイルに適切な内容を読み書きすることができるようになります。ここで

プログラムは3番目のファイルに

12 23 34 45 56 67 69 123 133 4 5 10 20 35 44 100 130 150 160 180 

を出力します何である私は、それは事前にそう

4 5 10 12 20 23 34 35 44 45 56 67 69 100 123 130 133 150 160 180 

感謝:)

+0

3番目のファイルには、それが印刷されることは印刷されません。あなたのコードを実行し、3番目のファイルに 'nullnull'が含まれています。すべての数字を改行すると、 'null123674523null16013044205'が入ります。 – Arjan

+0

これは私に質問につながります。それらの数字はすべて1行に入っているのですか? – Arjan

+0

両方のデータ番号をテキストファイルの2行目に置き、3番目の行に正しく出力する必要があります。また、3番目のファイルに出力する番号は、すべて1行になります。 –

答えて

0
ように、第3のファイルにそれをソートしたいです

Collections.sort(values);に電話すると、空のソートが行われますArrayList

ArrayListにすべての値を追加した後にCollections.sort(values);に電話する必要があります。

例1

List<Integer> values = new ArrayList<Integer>(); 

// sort the list first 
Collections.sort(values); 

values.add(5); 
values.add(3); 
values.add(8); 
values.add(1); 

System.out.println(values); 

出力:[5、3、8、1]

例2

あなたがテストにこれらの例を実行することができ

List<Integer> values = new ArrayList<Integer>(); 

values.add(5); 
values.add(3); 
values.add(8); 
values.add(1); 

// sort the list now 
Collections.sort(values); 

System.out.println(values); 

出力:[1、3、5、8]

+1

それだけでは修正されませんが、このコードにはさらに問題があります。 – Arjan

+0

各番号をソースコード内で個別に追加するのではなく、テキストファイルから直接値を追加できますか? –

+0

これは 'scan.hasNextInt()'のように解析する必要があります。 –

1

私はあなたがTreeSetのを使用することをお勧め、と私はあなたのコード内でいくつかの問題を見ました。

TreeSet<Integer> treeSet = new TreeSet<Integer>(); 
while(scan.hasNextInt()) treeSet.add(scan.nextInt()); 
scan = new Scanner(new File("D://School//text2.txt")); 
//Do not new Collection!!!!! 
while(scan.hasNextInt()) treeSet.add(scan.nextInt()); 

、あなたがのために、各コレクション

for (Integer i : treeSet) { 
     System.out.println(i); 
} 

することができますし、jdk8を持っている場合は、これを試すことができます。

ArrayList<Integer> ints = new ArrayList<>(); 
    Files.readAllLines(Paths.get("D://School//text1.txt"), StandardCharsets.UTF_8).forEach(x -> Arrays.asList(x.split(" ")).stream().forEach(y -> ints.add(Integer.parseInt(y)))); 
    Files.readAllLines(Paths.get("D://School//text2.txt"), StandardCharsets.UTF_8).forEach(x -> Arrays.asList(x.split(" ")).stream().forEach(y -> ints.add(Integer.parseInt(y)))); 
    Collections.sort(ints); 
    StringBuilder sb = new StringBuilder(); 
    ints.stream().map(x -> x + " ").forEach(sb::append); 
    Files.write(Paths.get("D://School//text3.txt"),sb.toString().getBytes(),StandardOpenOption.WRITE,StandardOpenOption.CREATE); 
+1

注:テキストファイルに重複がある場合は、TreeSetを使用してこの情報を失います。 –

+0

非常に素晴らしいラムダソリューション!残念ながら、text1/text2ファイルに空白の最初の行がある場合(質問のコメントのOPで説明されているように)、NumberFormatExceptionがスローされます。しかし、tbh、私は本当にその空行が代入の一部であったのか、OPの回避策がデータをバグのあるコードに適合させるのか疑問に思っています:P +1どちらかの方法:) – Arjan

+0

はい、空白行が回避策でした。私はまだJavaを学ぶ過程にいます。これまでのすべての助けを借りてくれてありがとうございます:) –

関連する問題