0

First some background:

The main method will drive your program by doing the following: •Create an array to hold all the individual golfers and par scores (type is Golfer[ ]). •Prompt the user for a data file containing the Par scores and the player names and score. The format of the input file should look like:

Par, 3, 4, 4, 3, 4, 3, 5, 3, 4

George, 3, 3, 4, 3, 4, 2, 3, 3, 4

Paul, 4, 5, 4, 3, 6, 3, 4, 3, 5

Ringo, 3, 3, 4, 3, 4, 2, 4, 3, 4

John, 4, 4, 4, 3, 4, 2, 5, 3, 4

This is what I have so far:

import java.io.File;
import java.io.IOException;
import java.util.*;

public class Main 

{

public static void main(String[] args)
{
    boolean tryAgain;

       do
       {
           Scanner console = new Scanner( System.in );
           System.out.print( "Please enter a file name to read: " );
           String inFile = console.next();
           tryAgain = false;

           try
           {
               File file = new File( inFile );     
               Scanner tokens = new Scanner( file ); 

                int data[][];
                String str = "";

               for (int i = 0; i < 5; i++)
               {
                   for (int j = 0; j < 10; j++)
                   {

                       int value = tokens.nextInt();


                   }
                   System.out.printf("\n");
               }

               tokens.close();

               String people [] = str.split(",");
               Golfer golfers = new Golfer(null, null);
           }


           catch (IOException e)
           {
               System.out.printf("Error opening file %s, %s\n", inFile, e);
               tryAgain = true;
           }
       }
       while (tryAgain);


}

I am unsure of how to properly use a nested for loop to pull the data and store it in an array since there are two data types. And... I guess any suggestions on how to store the information in my Golfer class couldn't hurt. I am a beginner so any complicated techniques should probably be avoided.

Any advice would be greatly appreciated. Thanks!

3
  • 2
    You need a single type; Golfer. Perhaps write a method to parse a single line into a Golfer. Commented Dec 4, 2014 at 2:35
  • put it on a Object then use instanceof in a condition :) Commented Dec 4, 2014 at 2:43
  • I am sorry but I am not quite sure how to do either of those things properly. Commented Dec 4, 2014 at 2:43

2 Answers 2

2

In this case, a single time needs to be created to hold all of the data from a single line, such as name, and an array to hold all of the scores for that particular golfer.

while(tokens.hasNext()) {
    String input = token.nextLine();
    String [] splitGolfer = input.split(",");
    Golfer newGolfer = new Golfer();
    newGolfer.name = splitGolfer[0];
    ArrayList<Integer> scores = new ArrayList<Integer>();
    for(int i= 1; i < splitgolfer.length; i++) {
        scores.add(Integer.valueOf(splitGolfer[i]);
    }
    newGolfer.scores = scores;

Golfer class:

String name;
ArrayList<Integer> scores;
Sign up to request clarification or add additional context in comments.

2 Comments

You may want to split by ", "
Is there any way to do this without using ArrayList?
0

Here is the suitable solution for you. Change variables, filename and directory as per your requirement. It will throw an exception if any line in the file will have a different format than what you have mentioned above (player's name followed by scores).

import java.util.*;
import java.io.*;

public class Golfer
{
    //arraylist of all lines from given files (players data).
    ArrayList<String> theData = new ArrayList<String>();
    private String playerName;
    private ArrayList<Integer> parScores;

    //you must read each line in the golder file and send it individually to this method.
    public Golfer() throws IOException
   {
        String fileName = "players.txt";
        theData = readFile(fileName);
        for (String s : theData)
        {
            getPlayerData(s);
        }
    }

    public void getPlayerData(String data)
    {
        String[] list = data.split(",");
        playerName = list[0];
        parScores = new ArrayList<Integer>();
        for( int i = 1; i < list.length; i++)
        {
            parScores.add(Integer.parseInt(list[i]));
        }
    }

    //This method will read your data file and will return arraylist (one array for each player).
    public ArrayList<String> readFile(String fileName) throws IOException
    {
        ArrayList<String> data = new ArrayList<String>();
        //don't forget to add directory name here if you have, because we are only passing filename, not directory.
        BufferedReader in = new BufferedReader(new FileReader(fileName));

        String temp = in.readLine(); 
        while (temp != null)
        {
            data.add(temp);
            temp = in.readLine();
        }
        in.close();
        return data;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.