Reading from a command line to an array argument (Java)

Platform = Windows XP
Java version = >1.5

I'm trying to make an alternate version of my Football Fantasy program. In this second version, I want to be able to get names of players via a command line, and create an array of rosters. So I guess each player's name would create an element in the array for a roster. Can someone help me with how to assign the member field with the name of the player read in from the command line?

Here is what my code looks like now for the driver program:

// Import Java package
import java.io.*;
import java.util.*;

// Class name
public class FootballScores
{
          BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
          public static void main(String[] args) throws IOException
          {    
               // Create an array of rosters & get input from an array argument (not keyboard)
               Roster[] arrayOfRoster = new Roster(args.length];
               for (int i = 0; i < args.length; i++)
               System.out.println("Arg #" + i + " = " + args[i]);
               {
                    Roster[i] = new Roster(args[i]); //declare & initialize a 1-dimensional array
               }
          }

Also, is this all the code necessary to get input from the command line, and could you maybe take a wee small second to explain the logic behind reading input from the command line, e.g., how does Java do it?

Thanks for your prompt assistance.
computereseAsked:
Who is Participating?
 
CEHJCommented:
You can use a Scanner to get the players. Use a List to store the names as you need an expandable form of storage:


List<String> players= new ArrayList<String>();
Scanner s= new Scanner(System.in);
while (s.hasNext()) {
    String player = s.next();
    players.add(player);
}
0
 
CEHJCommented:
What format would this roster take?
0
 
computereseAuthor Commented:
@CEHJ


Well, the array would be populated with instances of the players' names. It's a one-dimensional array and I guess the console output would look something like:

Player's name
(Some other functions I'm going to add)


This would be repeated for each player in the array.

Hope this gives you enough info. Also, to follow up on the original question, is there additional coding for the driver program so that it "knows" to get its input from the command line, or is there some coding that is added at the Command Prompt to direct the inputted names to the driver program?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
computereseAuthor Commented:
@ CEHJ

I was wondering if you could take just a moment & help me understand the mechanics of command prompt input & how Java receives such input. I have added the above Scanner code you provided into a method in my class file and a call to that method in my driver program. Now, at the DOS C prompt, do I need to: (1) do a cd (change directory) to locate the driver file, and then type java + file name? or (2) locate the class file? I'm getting a DOS error "Exception in thread "main" java.lang.NoClassDefFoundError:".  I'm sorry but this is my first time attempting to get input from the command line, so I'm not clear on how to actually get the command line input into the driver or class file.

Thanks for your continued patience.
0
 
CEHJCommented:
>>Now, at the DOS C prompt, do I need to: (1) do a cd (change directory) to locate the driver file, and then type java + file name?

You can do that, or put the class in your classpath

java FootballScores
0
 
objectsCommented:
Add the directory that contains your class file to the classpath
you can either do this by adding it to the CLASSPATH environment variable or specifiying it on the command line

eg. if FootballScores.class is in \projects\classes

java -classpath \projects\classes FootballScores

for more info

http://mindprod.com/jgloss/classpath.html
0
 
Mayank SAssociate Director - Product EngineeringCommented:
If you have no class-path, simply running it from the directory containing the class-file should do (though I personally prefer using the class-path).
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.