2016-05-04 65 views
1

我正在嘗試使用android對文本文件中的數字進行排序。你可以幫我嗎?這段代碼不是全部排序的部分。我的意思是它排序1.A 2.C 20.D 3.C 4.A 5.A那樣。它應該是1.A 2.C 3.C 4.A 5.A 6.B 20.D我需要轉換整數,但我該怎麼做?在文本文件中對數字進行排序android

嘗試 {

  File root = new File(Environment.getExternalStorageDirectory(), "Notes"); 
      if (!root.exists()) 
      { 
       root.mkdirs(); 
      } 
      File gpxfile = new File(root, fileName); 
      FileWriter writer = new FileWriter(gpxfile,true); 
      writer.append(s+"\n\n"); 
      writer.flush(); 
      writer.close(); 
      FileReader fileReader = new FileReader(gpxfile); 
      BufferedReader bufferedReader = new BufferedReader(fileReader); 
      String inputLine; 
      List<String> lineList = new ArrayList<String>(); 
      while ((inputLine = bufferedReader.readLine()) != null) { 
       lineList.add(inputLine); 
      } 
      fileReader.close(); 
      Collections.sort(lineList); 
      System.out.println(lineList); 
      FileWriter fileWriter = new FileWriter(gpxfile); 
      PrintWriter out = new PrintWriter(fileWriter); 
      for (String outputLine : lineList) { 
       out.println(outputLine); 
      } 
      out.flush(); 
      out.close(); 
      fileWriter.close(); 
      //Toast.makeText(this, "Data has been written to Report File", Toast.LENGTH_SHORT).show(); 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 
+0

你在哪裏排序數字? – Mohit

+0

@Mohit在文本文件中,我應該排序。 Collections.sort我使用它,但它排序爲一個字符串,因此1. 2. 20. 3 ...不是1. 2. 3. 20但我該怎麼做? – xiac

+0

嘗試使用'Set'來代替 – Mohit

回答

3

您正在分揀String,你需要排序Interger

  1. 解析「1.A 20.A 30.A 20.C 2.B 3 .A「

由於看起來您的輸入文件包含字母和數字的組合就像「1.A 20.A 30.A 2.B 20.C 3.A」你可以創建一個新類,實現Comparable來定義你自己的排序。

class Item implements Comparable<Item>{ 
    String stringPart; 
    Integer intPart; 
    String c; 

    public Item(String c){ 
     this.c = c; 
     String[] res = c.split("\\."); 
     intPart = Integer.valueOf(res[0]); 
     stringPart = res[1]; 
    } 

    @Override 
    public int compareTo(Item o) { 
     if(intPart.intValue() == o.intPart.intValue()){ 
      return stringPart.compareTo(o.stringPart); 
     }else{ 
      return intPart.compareTo(o.intPart); 
     } 
    } 

    public String getContent(){ 
     return c; 
    } 
} 

一旦這樣做,你可以根據新創建的類更改列表definiton:

List<Item> lineList = new ArrayList<>(); 

的,你可以分析輸入線填充列表

List<Item> lineList = new ArrayList<>(); 
while ((inputLine = bufferedReader.readLine()) != null) { 
    String[] res = inputLine.split(" "); 
    for(String s : res){ 
     lineList.add(new Item(s)); 
    } 
} 

排序列表內容通話:

Collections.sort(lineList); 

寫輸出文件:

for (Item i : lineList) { 
    out.println(i.getContent()); 
} 

輸入

"1.A 20.A 30.A 2.B 20.C 3.A"

會產生

1.A 2.B 3.A 20.A 20.C 30.A

  • 解析「A.1 A.20甲.30 B.2 C.20 A.30「
  • 如果輸入內容切換字符和數字是這樣的「A.1 A.20 A.30 B.2 C.20 A.30」您只需修改以下內容即可首先對字符和然後在數字:

    Item

    intPart = Integer.valueOf(res[1]); 
    stringPart = res[0]; 
    

    內容的閱讀和排序方法:

    @Override 
    public int compareTo(Item o) { 
        if(stringPart.equals(o.stringPart)){ 
         return intPart.compareTo(o.intPart); 
        }else{ 
         return stringPart.compareTo(o.stringPart); 
        } 
    } 
    
    +0

    這部分應該是字符串,當我寫你的時候給它一個錯誤while((inputLine = bufferedReader.readLine())!= null)lineList.add(inputLine ); } – xiac

    +0

    'lineList.add(Integer.parseInt(inputLine));' – aioobe

    +0

    @ aioobe它關閉應用程序 – xiac

    0

    正如上面說你正在字符串進行排序。因此,部分

     List<String> lineList = new ArrayList<String>(); 
         while ((inputLine = bufferedReader.readLine()) != null) { 
          lineList.add(inputLine); 
    
         } 
         fileReader.close(); 
         Collections.sort(lineList); 
    

    擁有的東西要變了樣:

     Map<Integer, String> list = new TreeMap<>(); 
         while ((inputLine = bufferedReader.readLine()) != null) { 
          System.out.println("Read string is: " + inputLine); 
          String[] entries= inputLine.split("\\s+"); 
          for(String entry: entries) { 
           String values[] entry.split("."); 
           if (values.size == 2) { 
            // Convert first part in integer  
            if (values[0]!=null && !values[0].isEmpty()) { 
            System.out.println("Converting string " + value[0] + " to integer"); 
            list.put(Integer.valueOf(value[0]), value[1]); 
           } 
          } 
         } 
         fileReader.close(); 
    
         Stream<Map.Entry<K,V>> sorted = 
           list.entrySet().stream() 
           .sorted(list.Entry.comparingByValue()); 
    

    注:沒有嘗試過自己上面的代碼,所以有可能是與它的問題,但它應該(希望)澄清需要做什麼。我也不完全確定像2.這樣的字符串是否已正確轉換。

    +0

    它關閉應用程序。我認爲我的問題是拆分方法。我的輸出1.A 2.B 20.C 3.A在文本文件中。 inputLine.split(「。」)再次嘗試關閉應用程序。我應該寫什麼 – xiac

    +0

    是否有例外?可能讀取的輸入行不包含任何字符。您可以在循環中添加'System.out.println(「Read:」+ inputLine);'。 – uniknow

    +0

    字符串'418a7f40'看起來不像您輸入的示例輸入,('1. 2. 20. 3. 4. 5.')。你確定你正在閱讀正確的文件,並且它包含你所顯示的數字嗎? – uniknow

    相關問題