2017-04-07 24 views
1

我正在嘗試創建一個類,該類使用具有相同格式,「#order)score - Name」的每行讀取文本文件。我希望能夠從文件中讀取每一行並將其存儲在一個數組中,然後按最高分對其進行排序。當它僅僅是「#order」得分時,我能夠完成這個任務,但是將該字符串添加到混合中會使事情複雜化。該錯誤是嘗試在Java中使用自定義比較器時發生錯誤

Scoring.java:46: error: no suitable method found for sort(ArrayList<String>,<anonymous Comparator<String>>) 
      Arrays.sort(scores, new Comparator<String>(){ 
       ^
method Arrays.<T#1>sort(T#1[],Comparator<? super T#1>) is not applicable 
    (cannot infer type-variable(s) T#1 
    (argument mismatch; ArrayList<String> cannot be converted to T#1[])) 
method Arrays.<T#2>sort(T#2[],int,int,Comparator<? super T#2>) is not applicable 
    (cannot infer type-variable(s) T#2 
    (actual and formal argument lists differ in length)) 
where T#1,T#2 are type-variables: 
T#1 extends Object declared in method <T#1>sort(T#1[],Comparator<? super T#1>) 
T#2 extends Object declared in method <T#2>sort(T#2[],int,int,Comparator<? super T#2>) 

的文本文件是:

1) 10000 - Michael 
2) 10000 - Jake 
3) 10000 - Alex 

和類是:

import java.io.*; 
import java.util.Arrays; 
import java.util.ArrayList; 
import java.util.Comparator; 
public class Scoring 
{ 
    private ArrayList<String> scores = new ArrayList<String>(); 
    public Scoring() 
    { 
     this(-1, "-1"); 
    } 
    public Scoring(int newScore, String newName) 
    { 
     String highScoreFile = "scores.txt"; 
     String line; 

     BufferedReader reader = null; 
     try 
     { 
      File file = new File(highScoreFile); 
      reader = new BufferedReader(new FileReader(file)); 
     } 
     catch (FileNotFoundException fnfe) 
     { 
      fnfe.printStackTrace(); 
     } 

     finally 
     { 
      try 
      { 

       while ((line = reader.readLine()) != null) 
       { 
        String aScore = line; 
        String segments[] = aScore.split(") "); 
        aScore = segments[segments.length - 1]; 
        scores.add(aScore); 
       } 
       if (newScore != -1 && !newName.equals("-1")); 
       { 
        scores.add(newScore + " - " + newName); 
       } 
       System.out.println(Arrays.toString(scores.toArray())); 
       Arrays.sort(scores, new Comparator<String>(){ 
        @Override 
        public int compare(String o1, String o2) 
        { 
         return Integer.valueOf(o1).compareTo(Integer.valueOf(o2)); 
        } 
       }); 

       BufferedWriter writer = null; 
       try 
       { 
        File file = new File(highScoreFile); 
        FileWriter fw = new FileWriter(file); 
        writer = new BufferedWriter(fw); 

       } 
       catch (FileNotFoundException fnfe) 
       { 
        fnfe.printStackTrace(); 
       } 
       for (int i = 0; i < scores.size(); i++) 
       { 
        writer.write((i + 1) + ") "); 
        writer.write(scores.get(i) + "\n"); 
       } 
       System.out.println(Arrays.toString(scores.toArray())); 
       reader.close(); 
       writer.close(); 
      } 
      catch(IOException ioe) 
      { 
       ioe.printStackTrace(); 
      } 
     } 
    } 
    public static void main(String[] args) { 
     Scoring score = new Scoring(); 


    } 
} 

任何反饋是非常感謝!

+0

請不要你的問題不會改變成新的。如果你有新的問題,即使它與已經發布的代碼有關,你應該在[單獨的問題](http://stackoverflow.com/questions/ask)中詢問它。 – Pshemo

+0

無論如何'NumberFormatException:對於輸入字符串:「200 - Jake」'很清楚。你試圖在字符串「200 - Jake」上使用'Integer.parseInt',這個字符串不是有效的數字。你可能只想解析'200'部分,所以你需要將它從字符串中分離出來。 – Pshemo

+0

我沒有跟隨,如果我將200從字符串中分離出來,那麼我是不是會失去附在數字上的名字? – shirkyy

回答

3

Arrays.sort期待T[]但您提供ArrayList這不是數組,它是一個內部使用數組來保存元素的列表。

改爲使用Collections.sort,或者從Java 8開始,您可以使用yourList.sort(comparator)


split BTW方法是使用正則表達式(正則表達式)作爲參數,其中)是正則表達式special characters之一。如果你要正確對待)爲文字,你需要(在Escape (in regular expression更多信息),以逃避它

所以不是aScore.split(") ");可以使用aScore.split("\\) ");

+0

那樣得到解決方案。你似乎是半工作的解決方案,但現在它是拋出一個新的錯誤,我將在主帖中提供! – shirkyy

相關問題