2014-01-08 51 views
0

嗨我想按照出現的降序從錯誤消息的用戶輸入文件進行排序。如何查找所有錯誤消息並按降序顯示

input_file.txt

23545 debug code_to_debug 
43535 error check your code 
34243 error check values 
32442 run program execute 
24525 error check your code 

我想輸出

error check your code 
error check values 

我當前的代碼:

import java.io.*; 
import java.util.*; 

public class Sort { 

    public static void main(String[] args) throws Exception { 
     BufferedReader reader = new BufferedReader(new FileReader("fileToRead")); 
     Map<String, String> map=new TreeMap<String, String>(); 
     String line=""; 
     while((line=reader.readLine())!=null){ 
      map.put(getField(line),line); 
     } 
     reader.close(); 
     FileWriter writer = new FileWriter("fileToWrite"); 
     for(String val : map.values()){ 
      writer.write(val); 
      writer.write('\n'); 
     } 
     writer.close(); 
    } 

    private static String getField(String line) { 
     return line.split(" ")[0];//extract value you want to sort on 
    } 
} 
+0

你到目前爲止的代碼在哪裏? –

+0

你可以使用Perl或紅寶石?這是任何一種語言的3線程序。 – Gene

回答

0

將您的映射從<String, String>更改爲<Integer, String>。然後,使用自定義Comparator來比較Integers從最小到最大。

看來您的錯誤消息按照從最嚴重到最不嚴重的整數值排列。這應該允許你使用這個事實。

+0

我不認爲這會產生他們之後的輸出......有一個「錯誤檢查您的代碼」與最高的整數值和第二低的整數值 –

+1

我認爲(相當強烈),成爲一個問題那麼你放入地圖。或者,有一個比較清晰的編號方案,比如那兩個錯誤*應該*相等。 – Makoto

+0

是的,我同意,編號順序似乎沒有邏輯含義(可能對OP)。 –

0

而不是有一個Map<String,String>其中關鍵是整數值,你可以有關鍵作爲錯誤消息,然後該值可以包含一個整數值的列表,所以當閱讀文件時,它會變成類似的東西,也實現在地圖比較命令他們:

Map<String, String> map = new TreeMap<String, List<String>>(new Comparator<String>() 
    { 
     @Override 
     public int compare(String s1, String s2) 
     { 
      //Implement a compare to get the order of string you want 
     } 
    } 
); 
String line = ""; 
while((line = reader.readLine()) != null) 
{ 
    String lineStr = line.split(" ")[1]; // get the message 
    List<String> vals = map.get(lineStr) // get the existing list 
    if(vals == null) 
      vals = new ArrayList<String>(); // create a new list if there isn't one 
    vals.add(getFeild(line));   // add the int value to the list 

    map.put(lineStr,vals);    // add to map 
} 

然後,您可以對列表進行排序成數字順序,如果你想要的。此外,這將需要更多的工作來打印出地圖 - 但這取決於格式

0

如果您只想對輸入重新排序以便所有錯誤消息出現在頂部,則可以使用一種非常簡單的方法它是這樣的:

static String[] errorsToTop(String[] input) { 
    String[] output = new String[input.length]; 

    int i = 0; 
    for(String line : input) { 
     if(line.contains("error")) 
      output[i++] = line; 
    } 

    for(String line : input) { 
     if(!line.contains("error")) 
      output[i++] = line; 
    } 

    return output; 
} 

這只是複製數組首先與所有錯誤消息,然後將所有非錯誤消息。

雖然邏輯不太明顯,但也可以使這兩個循環成爲嵌套循環。

static String[] errorsToTop(String[] input) { 
    String[] output = new String[input.length]; 

    int i = 0; 
    boolean not = false; 
    do { 
     for(String line : input) { 
      if(line.contains("error")^not) 
       output[i++] = line; 
     } 
    } while(not = !not); 

    return output; 
} 

目前還不清楚數字是否出現在您的輸入文本文件中。如果他們不這樣做,你可以使用startsWith代替contains

if(line.startsWith("error")) 

你也可以使用matches與像一個正則表達式:

if(line.matches("^\\d+ error[\\s\\S]*")) 

它說「打頭的任意整數後跟一個空格,然後錯誤之後是什麼也不是「。

0

由於沒有答案已被標記,我會加2美分。 下面的代碼適用於您發佈的內容(也可能沒有其他內容),它假定錯誤的數量高於非錯誤的數量,並且您根據時間片或某物抓取最多N行。

import java.util.NavigableMap; 
import java.util.TreeMap; 

public class SortDesc { 

    public static void main(String[] args) { 
    NavigableMap<Integer, String> descendingMap = new TreeMap<Integer, String>().descendingMap(); 
    descendingMap.put(23545, "debug code_to_debug"); 
    descendingMap.put(43535, "error check your code"); 
    descendingMap.put(34243, "error check values"); 
    descendingMap.put(32442, "run program execute"); 
    descendingMap.put(24525, "error check your code"); 

    System.out.println(descendingMap); 
    } 
} 

結果是這樣的

{43535 =錯誤檢查你的代碼,34243 =錯誤校驗值,32442 =運行程序執行,24525 =錯誤檢查你的代碼,23545 =調試code_to_debug}