0

I'm writing this golf program for my class that takes a .txt that has 5 numbers per row and 18 rows. The first number in each row is the par for that hole. The other four numbers are for each player.

I kind of have my own spin on this program, though. I wrote another program to create the .txt file with random numbers. The output is dependent on how many players there are which is inputted by the user.

Anyway, I've gotten the .txt generator just fine and I've gotten the Golf program to accurately count how many players there are. What I can't figure out is how to create that number of arrays with different names. I want each array to be int player1[18], player2[18], player3[18], etc.

Here's my code up to that point:

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

public class Golf
{
    public static void main(String[] args) throws java.io.IOException
    {
        Scanner countScan = new Scanner(new File("golfscores.txt"));
        Scanner file = new Scanner(new File("golfscores.txt"));

    //------------------------------------------------------------
    // Counting the number of players
    //
    // This takes the number of integers in the file, divides it by
    // the 18 holes in the course and subtracts 1 for the par.
    //
    // I needed to count the players because it's a variable that
    // can change depending on how many players are entered in the
    // java program that creates a random scorecard.
    //------------------------------------------------------------
        int players = 0;

        for (int temp = 0; countScan.hasNextInt(); players++)
            temp = countScan.nextInt();
        players = players/18-1;

    //------------------------------------------------------------
    //Creating necessary arrays
    //------------------------------------------------------------



    }
}

EDIT: I must use an array for each player and I am not allowed to use ArrayLists. At this point it looks like I will be using an array of arrays as suggested by some in the comments. Didn't know this was a thing (obviously I'm very noob).

4
  • 4
    Why not an array of arrays? Commented Mar 3, 2016 at 19:39
  • Making an array of arrays (matrix) is what comes to mind. Commented Mar 3, 2016 at 19:40
  • Looks like I'll probably be going with an array of arrays. Didn't know that existed! Thank you. Commented Mar 3, 2016 at 19:46
  • Array of arrays is also known as a multidimensional array - docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html Commented Mar 3, 2016 at 19:49

2 Answers 2

1

Well you can use a HashMap to store your arrays. Or if you don't care about using strings to get to the array just use 2D Arrays like this:

int[][] players = new int[playerCount][18];

If you then use for example player 2 and want to see hole 12, you'd call players[1][11]

Sign up to request clarification or add additional context in comments.

1 Comment

This looks like exactly what I need! Thank you! I think others that commented about array of arrays had this same thing in mind. =)
0

You should not go that way.

Instead create a class named Player to hold each player properties, then create a list of players: List<Player> players = new ArrayList<>();

Add each new player to that list.

2 Comments

Well, the assignment specifically says that I need an array for each player and that I can't use an ArrayList. =( Sorry, I will update my question to include that information.
OK, it's an assignment about array manipulation. So good luck ;)

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.