2013-04-25 56 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