2013-04-25 59 views
0

我想弄清楚如何使用數組讀取文件中的數據。文件中的數據如下所示:如何用讀取文件數據實現數組處理?

Clarkson 80000 
Seacrest 100000 
Dunkleman 75000 
... 

我想使用數組存儲該信息。目前,我有這樣的事情來讀取數據,並使用它:

  String name1 = in1.next(); 
      int vote1 = in1.nextInt(); 


      //System.out.println(name1 +" " + vote1); 

      String name2 = in1.next(); 
      int vote2 = in1.nextInt(); 

      //System.out.println(name2 +" " + vote2); 

      String name3 = in1.next(); 
      int vote3 = in1.nextInt(); 
       ... 
       //for all names 

問題是,我做的方式意味着我永遠無法操縱文件數據的多個競賽或諸如此類的東西。 雖然我可以使用這種方式,並處理不同方法中的所有數學,並獲得預期的輸出......我認爲它效率非常低。

輸出預計:

American Idol Fake Results for 2099 
Idol Name  Votes Received % of Total Votes 
__________________________________________________  
Clarkson   80,000   14.4% 
Seacrest   100,000   18.0% 
Dunkleman   75,000   13.5% 
Cowell    110,000   19.7% 
Abdul    125,000   22.4% 
Jackson    67,000   12.0% 

Total Votes   557,000 

The winner is Abdul! 

我想讀取輸入文件中的數據到數組很可能容易使用java.io.BufferedReader有沒有辦法不使用呢? 我看着這個:Java: How to read a text file但我堅持認爲這是一個不同的實現。

我想嘗試通過可理解的數組處理所有信息,也許至少有2-3個方法(除讀取和存儲運行時的所有數據的主要方法外)。但是,說我想使用這些數據,並找到百分比和東西(如輸出)。找出勝利者......甚至可以按字母順序排列結果!

我想嘗試一些事情,並瞭解代碼如何工作以獲得手頭概念的感受。 ; c

回答

0
int i=0 
while (in.hasNextLine()) 

    { 

     name = in.nextLine(); 
     vote = in.nextInt(); 


     //Do whatever here: print, save name and vote, etc.. 
     //f.e: create an array and save info there. Assuming both name and vote are 
     //string, create a 2d String array. 
    array[i][0]=name; 
    array[i][1]=vote; 

    //if you want to individually store name and votes, create two arrays. 
    nameArray[i] = name; 
    voteArray[i] = vote; 
    i++; 

    } 

這將循環直到他自動發現你沒有更多的行要閱讀。在循環內,你可以做任何你想做的事情(打印名字和投票等)。在這種情況下,您將所有值保存到數組[] []中。

陣列[] []將是這樣的:

array[0][0]= Clarkson 

array[0][1]= 80,000 

array[1][0]= Seacrest 

array[1][1]= 100,000 

...等等。

此外,我可以看到你必須做一些數學。所以,如果你將它保存爲一個字符串,你應該將其轉換爲這樣的兩倍:

double votesInDouble= Double.parseDouble(array[linePosition][1]); 
+0

那不會不斷地用下一行/ int代替name/vote,直到它到達文檔的結尾? – 2013-04-25 22:49:47

+0

不,如果你打印它。如果你想保存它,你也可以做到。我會編輯答案。 – 2013-04-25 22:50:12

+0

好的,我看到你的編輯。如果我正確地看它:它會這樣做直到讀取文件中的所有數據。說'name2'是用'vote2'打印的,數組是如何命名的?或者我以後如何使用這些信息來操作 – 2013-04-25 22:58:18

1

您有幾種選擇:

  1. 創建一個類來表示你的文件數據,然後有一個這些對象

  2. 的陣列保持平行的兩個陣列,名字的一個和另一個的票

  3. 使用一個Map,該人的名字是關鍵和的票數是

    一)讓你像一個數組

    B)你不需要創建一個類直接訪問

選項1:

public class Idol 
{ 
    private String name; 
    private int votes; 

    public Idol(String name, int votes) 
    { 
     // ... 
    } 

} 

int index = 0; 
Idol[] idols = new Idol[SIZE]; 

// read from file 
String name1 = in1.next(); 
int vote1 = in1.nextInt(); 

//create Idol 
Idol i = new Idol(name1, vote1); 

// insert into array, increment index 
idols[index++] = i; 

選項2:

int index = 0; 
String[] names = new String[SIZE]; 
int[] votes = new int[SIZE]; 

// read from file 
String name1 = in1.next(); 
int vote1 = in1.nextInt(); 


// insert into arrays 
names[index] = name1; 
votes[index++] = vote1; 

方案3:

// create Map 
Map<String, Integer> idolMap = new HashMap<>(); 

// read from file 
String name1 = in1.next(); 
int vote1 = in1.nextInt(); 

// insert into Map 
idolMap.put(name1, vote1); 

現在你可以回去的任何數據操縱你的心內容。

+0

我在想一個類有幾種方法來處理:讀取文件的方法(保存所有內容,名稱和投票分開),格式化所有數據並打印它的方法,處理數據的方法(如查找整體百分比)...這樣的東西 ------編輯:注意到你正在編輯你的文章,我會刷新,如果有什麼新的彈出 – 2013-04-25 22:54:56

+0

@ InspiringWisdomhammer我認爲這可能是最乾淨的解決方案,我張貼其他人,因爲我不確定你想要什麼。 – 2013-04-25 22:56:12

+0

那麼,看看選項1,它讀取所有數據並保存它?就像稍後說的我想打印'name2'將不存在? – 2013-04-25 23:01:51