Lab 10d – Read Player Data

Modify PlayerTest.java so that it satisfies the following.


Add a method named getPlayerData. The method has a single parameter named fileName that holds a String.

The method attempts to create a Scanner that can read the file whose name is in fileName.

If successful, the method changes the Scanner’s delimiters by calling useDelimiter() so that it parses tokens separated by commas and new-line characters.

The method then reads the integer on the first line of the file and allocates an array of Player objects whose length is equal to value read from the file.

For each subsequent line of the file, the method reads the player’s username and level from the file, creates an instance of the Player class, and stores the reference to the new instance in the array of Player objects.

When finished reading the file, the method returns the array of Player objects.


In main, get the data in players.data by calling getPlayerData().  Print to the screen the contents of the array that is returned by getPlayerData() using printPlayer().


You know the drill.

Lab 10c – Save Player Data

Modify PlayerTest.java so that it satisfies the following.


Add a method named savePlayerData. The method has two parameters.  The first parameter is named arr and holds a reference to an array of Player objects. The second parameter is named fileName and holds a reference to a String.

The method attempts to create a PrintWriter which can write to the file whose name is stored in fileName.

If the PrintWriter is successfully created, the method first writes the length of arr  on the first line of the file.  Then, for each Player object in arr, the method writes the player’s name, a comma, and the player’s level on a single line.  Each player’s data should be printed on a separate line.


In main, save the data in the array of Players to a file named players.data using the savePlayerData method.


Compile, test, debug, repeat.  Verify the data is properly saved in players.data.  When satisfied, push.

Lab 10b – Test the Player

Create a file named PlayerTest.java inside your lab10 directory.  Modify PlayerTest.java so that it satisfies the following requirements.


Include a method named printPlayers that takes an array of Player objects as an argument. For each Player object in the array, the method prints the player’s name, a comma, and the player’s level on a single line.

Inside main:

    • Create an instance of Player so that it models a player whose name is “Jamel” and whose level is 3.
    • Create an instance of Player so that it models a player whose name is “Keelie” and whose level is 12.
    • Create an array that can hold 2 players and add the two instances described above to the array.
    • Print all of the players’ names and levels by calling printPlayers.

Compile, test, debug, repeat.  When satisfied that your program is correct, push you .java files to GitHub.

Lab 10a – Model a Player

Log into cs.bridgewater.edu and navigate to your csci101/labs directory.  Create a directory named lab10.  Inside your lab10 directory, create a file named Player.java. The Player class should model a player in a game application. Edit Player.java so that is satisfies the following specifications:


    • Include a field named username that stores a String holding the player’s username.  The fields should be initialized to null.
    • Include a field named level that stores an integer holding the player’s level in the game.  The field should be initialized to 1.
    • Include a constructor that has two parameters.  The first parameter is named name and holds a String and the second parameter is named level and holds an integer.
      • If the value in the parameter name is null or the empty string then the constructor sets the name field to “error”; otherwise the constructor sets the name field equal to the value in the parameter name.
      • If the value in the parameter level is less than 1 then the constructor sets the level field equal to 1; otherwise the constructor sets the level field equal to the value in the parameter level.
    • Include getters and setters for the name and level fields.  In the setters, use the same input validation logic as described for the constructor.

Compile and when it compiles without errors, continue on to Lab 10b.