将字符串文件作为int读入数组?

时间:2016-11-06 01:05:45

标签: java arrays arraylist type-conversion

我需要编写一个程序,该程序接收一个输入文件,其中包含Golfer(practice.txt)的名称,年龄和分数信息。然后,该文件应将分数与特定年龄组的标准分数进行比较。例如:曼尼是14岁,因此他遵循12-15岁年龄组的标准分数。曼尼在第一洞打了5杆,所以他打出了标准杆。我无法理解如何比较文件和数组?任何让我上路的代码或想法都会很棒。

输入文件practice.txt:

Jay 57 4 3 2 3 5 3 2 3 4

Gloria 39 4 4 3 4 3 4 3 3 5

Manny 14 5 6 4 6 5 6 4 4 6

Joe 3 9 8 8 7 6 6 7 5 7

Par Parores Chart:

                           HOLES
AGE             1   2   3   4   5   6   7   8   9

4 and under     8   8   9   7   5   7   8   5   8
5 – 7           7   7   8   6   5   6   7   5   6
8 – 11          6   5   6   5   4   5   5   4   5
12 – 15         5   4   4   4   3   4   3   3   4
16 and over     4   3   3   3   2   3   2   3   3

我所拥有的:

import java.io.File;
import java.util.Scanner;
import java.util.List;
import java.io.FileNotFoundException;
import java.util.ArrayList;

public class imTryingHere {
 public static void main (String[] args) throws FileNotFoundException
  {

     int [][] ageGroups = 
    {
       {4},
       {7},
       {11},
       {15},
       {100},
    };

    int[][] holePars=
    {
      {8,8,9,7,5,7,8,5,8},
      {7,7,8,6,5,6,7,5,6},
      {6,5,6,5,4,5,5,4,5},
      {5,4,4,4,3,4,3,3,4},
      {4,3,3,3,2,3,2,3,3},
   };


  }
}

2 个答案:

答案 0 :(得分:1)

试试这个。

// Put age and par scores into Map
static Map<Integer, List<Integer>> ageParsList = new LinkedHashMap<Integer, List<Integer>>() {
    {put(4,  new LinkedList<Integer>(Arrays.asList(8, 8, 9, 7, 5, 7, 8, 5, 8)));}
    {put(7,  new LinkedList<Integer>(Arrays.asList(7, 7, 8, 6, 5, 6, 7, 5, 6)));}
    {put(11, new LinkedList<Integer>(Arrays.asList(6, 5, 6, 5, 4, 5, 5, 4, 5)));}
    {put(15, new LinkedList<Integer>(Arrays.asList(5, 4, 4, 4, 3, 4, 3, 3, 4)));}
    {put(-1, new LinkedList<Integer>(Arrays.asList(4, 3, 3, 3, 2, 3, 2, 3, 3)));}
};

public static List<Integer> getParScoresList(int age) {

    int targetAge = -1;
    for (Integer key : ageParsList.keySet()) {
        if (age <= key) {
            targetAge = key;
            break;
        }
    }

    return ageParsList.get(targetAge);      
}

public static class Player {
    String name;
    int age;
    List<Integer> scoreList;

    public Player(String name, int age, List<Integer> scoreList) {
        this.name = name;
        this.age  = age;
        this.scoreList = scoreList;
    }
}

private static List<Player> getPlayerScore() {
    Scanner scan = null;
    try{
          File file = new File(_path_to_file_);
          scan = new Scanner(file); 

          List<Player> playerList = new ArrayList<Player>();

          while (scan.hasNext()) {
              String name = scan.next();
              int age = scan.nextInt();
              List<Integer> scoreList = new LinkedList<Integer>();
              for (int i = 0; i < 9; i++) {
                  scoreList.add(scan.nextInt());
              }
              scan.nextLine(); // last line must have newline character.

              Player player = new Player(name, age, scoreList);
              playerList.add(player);
          }           

          return playerList;
    } catch(FileNotFoundException e){
        System.out.println(e);
        return null;
    } finally {
        if (scan != null) {
            scan.close();
        }
    }
}

public static void main (String[] args) {
    List<Player> playerList = getPlayerScore();

    for (Player player : playerList) {
        List<Integer> parScoresList = getParScoresList(player.age);

        for (int i = 0; i < 9; i++) {
            int score = player.scoreList.get(i);
            int par   = parScores.get(i);

            // Compare player's score and par score here.
        }
    }
}

答案 1 :(得分:0)

NIO's Files.newBufferedReader() is very good

所以它可能看起来像这样:

Path practiceFile = Paths.get("/tmp/practice.txt");
try (BufferedReader reader = Files.newBufferedReader(practiceFile, StandardCharsets.UTF_8)) 
{
    Pattern scorePattern = Pattern.compile("^(\\w+)([\\d\\s]+)");
    String line;
    while ((line = reader.readLine()) != null) {
        Matcher m = scorePattern.matcher(line.trim());
        if (m.matches())
        {
          String name = m.group(1);
          String[] scores = m.group(2).trim().split(" ");
          //do more stuff
        }
    }
} 
catch (Exception e) {
    e.printStackTrace();
}
相关问题