2016-11-06 19 views
0

ゴルファー(practice.txt)の名前、年齢、得点情報を入力ファイルに取り込むプログラムを作成する必要があります。このファイルは、特定の年齢層の得点と比較する必要があります。たとえば、マニーは14歳なので、12-15歳の年齢層のパースコアに従います。マニーはホール1で5を撃ったので、彼はパーになった。ファイルと配列を比較する方法を理解できませんでしたか?私の方法で私を得るためのコードやアイディアはすばらしいでしょう。文字列ファイルをint型として配列に読み込みますか?

入力ファイルpractice.txt:

ジェイ57 4 3 2 3 5 3 2 3 4

グロリア39 4 4 3 3 4 4 3 3 5

マニー14 5 6 4 6 5 6 4 4 6

ジョー3 9 8 8 7 6 6 7 5 7

パーのスコアがチャート:

      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}, 
    }; 


    } 
} 
+0

一般的な戦略は、目標を達成するのに役立つデータ構造にデータを読み込むことです。あなたがnakano531の答えを見ると、コードが大部分を占めていることが分かります。 "Par Scores Chart"はファイルから読み込まれるのではなく、ハードコードされていますが、値は後で年齢によって引き出すことが容易なように、Map構造に配置されています。同様に、演習ファイルからのデータはプレイヤーオブジェクトのリストに読み込まれ、後でアクセスして使いやすくなります。そして、両方の構造体は 'main'メソッドでアクセスされます。 –

答えて

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(); 
} 
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. 
     } 
    } 
} 
+0

良い答えですが、私は実際にはfor(int i = 0; i <9; i ++)のようなループのハードコーディングされた制限が嫌いです。私の次のポイントはより重要なものですが、 'Scanner'は少し使いにくく、この特別なケースではTong Liuが提案したNIOファイルの読み込みロジックよりもエレガントではありません。 –

関連する問題