Results 1 to 7 of 7
  1. #1
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    12
    Rep Power
    0

    Default Random automation of sports teams (things) using arrays, scanners, and randomNumbers

    So I was inspired by an example in my java 2 textbook and I have an idea for a java program but I'm not sure what the best approach would be. The example in the book is a program that deals a deck of 52 regular playing cards in a random order until the deck is empty using card objects.

    Basically I want to start with a plain .txt file of names of hockey players (up to a couple hundred) for the current year along with a single character representing their position played. Also, I'm assuming that each name is a unique entry. There would be no chance of duplicates. For instance a file named "players2011.txt" might contain:

    ...
    James Smith C
    Tony Watson F
    Sam Thompson G
    ...

    Ok, with that in mind I want to randomly draw 1 center (let's say, C), 2 forwards, 2 defensemen, and 1 goalie from all of the players in the league (in the .txt file). This could be the active team on the ice. I would have a scanner object read each line of the .txt file into arrays based on the last character in the line (C, F, G, ...).

    So the output could be:

    Frank Lewis C
    Tony Watson F
    Aaron Miller F
    Larry Fisher D
    Eric Gibson D
    Bobby Sanders G

    ..but with random players names each time using the same sequence of positions.

    My thoughts are, can I make a regular array object for each position and draw a player randomly from each as needed? After playing with it a little I found it is a little tricky to declare arrays without first knowing the size. Meaning how can I make an array of goalies if I don't know the number of goalies in the text file (this will vary year to year)? (Assume that I will have a new .txt file for each year.)

    Any thoughts?
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Sizzlewump View Post
    So I was inspired by an example in my java 2 textbook and I have an idea for a java program but I'm not sure what the best approach would be.
    All that you've mentioned sounds like a classic homework assignment rather than an "inspired program". It's best to be upfront about these things if you want our help. Else many may not trust you and could put on you their do-not-help list.

    The example in the book is a program that deals a deck of 52 regular playing cards in a random order until the deck is empty using card objects.

    Basically I want to start with a plain .txt file of names of hockey players (up to a couple hundred) for the current year along with a single character representing their position played. Also, I'm assuming that each name is a unique entry. There would be no chance of duplicates. For instance a file named "players2011.txt" might contain:

    ...
    James Smith C
    Tony Watson F
    Sam Thompson G
    ...

    Ok, with that in mind I want to randomly draw 1 center (let's say, C), 2 forwards, 2 defensemen, and 1 goalie from all of the players in the league (in the .txt file). This could be the active team on the ice. I would have a scanner object read each line of the .txt file into arrays based on the last character in the line (C, F, G, ...).

    So the output could be:

    Frank Lewis C
    Tony Watson F
    Aaron Miller F
    Larry Fisher D
    Eric Gibson D
    Bobby Sanders G

    ..but with random players names each time using the same sequence of positions.

    My thoughts are, can I make a regular array object for each position and draw a player randomly from each as needed? After playing with it a little I found it is a little tricky to declare arrays without first knowing the size. Meaning how can I make an array of goalies if I don't know the number of goalies in the text file (this will vary year to year)? (Assume that I will have a new .txt file for each year.)

    Any thoughts?
    Your best bet here is to use an ArrayList, fill it with objects of your Player class, then shuffle the ArrayList using the java.util.Arrays utility class's shuffle(...) method. Then you can remove the 0th Player until the ArrayList's size is 0. Calling remove(0) on the ArrayList will return the Player in the 0th position and will also remove that Player from the list.

    Or are you restricted from using ArrayLists and the shuffle method in this assignment?

  3. #3
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    12
    Rep Power
    0

    Default

    Thanks for the suggestion. I opted not to submit my code because there are about 7 different versions of it. This isn't for an assignment, I just have a genuine interest in java. Call me weird. One version uses arrayLists but does not fit nicely with the example code I'm working from which uses arrays. I haven't been able to find a way to dump the contents of an arrayList into an array, but it seems like there would be a way.
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The standard way to draw randomly without replacement is to shuffle and remove required number of elements from the top as has already been stated. Using random numbers to select array indices is more complicated, and therefore more error prone.

    You can get an array from an ArrayList instance with the ArrayList toArray() method.

  5. #5
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    12
    Rep Power
    0

    Default

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.lang.*;
    import java.util.*;
    
    public class Players
    {
       private Player[] team, centers, forwards, defensemen, goalies; 
    	private int currentPlayer;
    	private static final int NUMBER_OF_PLAYERS = 249; 
    	private static final Random randomNumbers = new Random();
    	static Scanner s;
       static File file;
    
       public Players() 
    	{
    	   file = new File ( "players2011.txt" );
    		int c, f, d, g; //element iterators for the corresponding player position array
          c = f = d = g = 0;
    	   try
    		{
    	      s = new Scanner( file );
    		}
    		catch ( FileNotFoundException e ) 
    		{
             e.printStackTrace();
    	   }
          while( s.hasNextLine() )
    	   {
    	      String name = s.nextLine();
    		   int nameLength = name.length();
    			char playerPosition = name.charAt( nameLength -1 );
    			String position = Character.toString( playerPosition );
             name = name.substring( 0, nameLength -2 );
             //these following lines are my failed attempt to add player objects to arrays 
    			//of unspecified sizes.
    			if( position.equals( "C" ) )
    			{
    				centers[ c ] = new Player( name );
    				c++;
    			}
    			else if( position.equals( "F" ) )
    			{
    				forwards[ f ] = new Player( name );
    				f++;
    			}
    			else if( position.equals( "D" ) )
    			{
    				defensemen[ d ] = new Player( name );
    				d++;
    			}
    			else if( position.equals( "G" ) )
    			{
       		   goalies[ g ] = new Player( name );
    				g++;
    			}
    			else if( position.equals( null ) )
    			{
    				System.out.println("that didn't work.");
    			}
    		}
    		s.close();
    	   //String[] team = { "a", "b", "c" }; 
    		
    		//partial code from original code to be modified at a later time...
    	   team = new Player[ NUMBER_OF_PLAYERS ]; 
    		currentPlayer = 0;
    
    	   for ( int count = 0; count < team.length; count++ ) 
    		   team[ count ] =
    			   new Player( team[ count % 3 ] ); 
    	}
    
       public void shuffle()
    	{
          currentPlayer = 0;
    
          for ( int first = 0; first < team.length; first++ ) 
    		{
             int second = randomNumbers.nextInt( NUMBER_OF_PLAYERS );
    			
             Player temp    = team[ first ];
    			team[ first ]  = team[ second ];
    			team[ second ] = temp;			
    		}
    	}
    
          public Player dealCard()
    		{
             if ( currentPlayer < team.length )
    			   return team [ currentPlayer++ ];
    			else
    			   return null;
    		}	
    }
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  6. #6
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    12
    Rep Power
    0

    Default

    Thank you. I'll give that .toArray() method a try in my other code. Here's the player class.
    Java Code:
    public class Player
    {
       private String name;
    
    	public Player( String playerName )
    	{
    	   name = playerName;
    	}
    	public String toString()
    	{
    	   return name; 
    	}
    }
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by Sizzlewump View Post
    I haven't been able to find a way to dump the contents of an arrayList into an array
    Why would you want to do that? Using Lists is much better than arrays.

    Also, shouldn't your Player class need to hold the position as well?

Similar Threads

  1. How to use arrays with Math.random
    By LennyKosmos in forum New To Java
    Replies: 2
    Last Post: 09-20-2010, 04:44 PM
  2. using random strings from arrays insult generator
    By carden2 in forum New To Java
    Replies: 5
    Last Post: 04-05-2010, 05:51 AM
  3. LUDecomposition, random arrays.
    By rootpi in forum New To Java
    Replies: 5
    Last Post: 12-23-2009, 03:32 PM
  4. Random numbers and arrays
    By caro in forum New To Java
    Replies: 6
    Last Post: 06-10-2009, 01:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •