Java - 数组和输入文件阅读器

时间:2014-05-14 02:52:42

标签: java arrays

程序编译但输出错误。在读取来自baseball.txt的所有数据后,程序应显示每个球员队伍编号,以及棒球总命中数,步行总数以及存储在文件输入行上的输出总数。

无法弄清楚我做错了什么。

// java class for file I/O
import java.io.*;
import java.util.*;

// declaration of the class
public class Baseball
{

    // declaration of main program
    public static void main(String[] args) throws FileNotFoundException
    {

    // 1. connect to input file
    Scanner fin = new Scanner(new FileReader("baseball.txt"));

    // objects used to store data

    int number = 0,         // number, hits, walks, outs
        hits, 
        walks,
        outs,
        players;


    // 2. output descriptive messages
    System.out.println("This program tracks a baseball player's number "
                         + "and their\nnumber of hits, walks and outs for "
                         + "each game in a season.\n");

    // 3. input the number of players

    players = fin.nextInt();

    // 4. declare an array of players
    Player[] teamArray = new Player[players];

    // 5. loop over teamsize
    // 5a. instantiate the i'th team member
    for(int i = 0; i < teamArray.length; i++){
        teamArray[i] = new Player();
    }


    // 6 loop on end of file
    while (fin.hasNext())
        {
        // 6a. input the team number
               number = fin.nextInt();
           // 6b. input the player's hits
               hits = fin.nextInt();
           // 6c. input the player's walks
               walks = fin.nextInt();
           // 6d. input the player's outs
               outs = fin.nextInt();
           // 6e. update player attribute hits
                teamArray[players-1].getHits();
                teamArray[players-1].setHits(hits);
           // 6f. update player attribute walks
                teamArray[players-1].getWalks();
                teamArray[players-1].setWalks(walks);
           // 6g. update player attribute outs
                teamArray[players-1].getOuts();
                teamArray[players-1].setOuts(outs);

       }


    // display the results
    System.out.println("\n\nPlayer\tHits\tWalks\tOuts\n" 
                      + "------\t----\t-----\t----\n");
    // 7. loop over team size
    for (int t = 0; t < teamArray.length; t++)
        if (teamArray[t] == null)
        {
            System.out.println(" " + (t+1) + "\t" + "0" + "\t" + "0" + "\t" + "0");
        }
        else
            System.out.println(" " + (t+1) + "\t" + teamArray[t]);


    // 8. disconnect from input file
    fin.close();

    } // end of main
} // end of the class 

这是Player.java

// declare the class Player 
public class Player {



    // declare the attribute variables
    private int hits;
    private int walks;
    private int outs;


    // implement the default constructor
    public Player(){
        hits = 0;
        walks = 0;
        outs = 0;
    }


    // implement an extractor for each attribute variable
    public int getHits()
    {
         return hits;
    }

    public int getWalks()
    {
        return walks;
    }

    public int getOuts()
    {
        return outs;
    }
    // implement a mutator for each attribute variable
    void setHits(int hitsInt)
    {
        hits = hitsInt + hits;
    }

    void setWalks(int walksInt)
    {
        walks = walksInt + walks;
    }

    void setOuts(int outsInt)
    {
        outs = outsInt + outs;
    }
    // overload toString to support output of attribute values
    public String toString()
    {
        String s = new String();
        s = hits + "\t" + walks + "\t" + outs;
        return s;
    }

  // end of the class 
}

这是Baseball.txt

20
1 2 2 2
20 0 5 1
2 0 0 6
18 4 2 0
3 2 1 3
4 1 2 3
7 0 0 3
8 1 4 1
9 3 2 1
10 2 2 2
11 6 0 0 
12 2 2 2
2 0 5 1
20 0 0 6
17 4 2 0 
4 2 1 3
3 1 2 3
7 0 0 3

2 个答案:

答案 0 :(得分:0)

您将所有数据存储到teamArray[player-1],而不是存储在每个播放器中。

while循环之外创建一个临时变量,并在每次迭代时递增计数器。

int counter = 0;
// 6 loop on end of file
while (fin.hasNext())
    {
    // 6a. input the team number
           number = fin.nextInt();
       // 6b. input the player's hits
           hits = fin.nextInt();
       // 6c. input the player's walks
           walks = fin.nextInt();
       // 6d. input the player's outs
           outs = fin.nextInt();
       // 6e. update player attribute hits
           teamArray[counter].setHits(hits);
       // 6f. update player attribute walks
           teamArray[counter].setWalks(walks);
       // 6g. update player attribute outs
           teamArray[counter].setOuts(outs);
           counter++;
   }


// display the results

答案 1 :(得分:0)

请使用此代码:

// 6e. update player attribute hits
teamArray[number-1].setHits(hits);
// 6f. update player attribute walks
teamArray[number-1].setWalks(walks);
// 6g. update player attribute outs
teamArray[number-1].setOuts(outs);