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

Posted on 2006-05-10
Last Modified: 2012-05-05
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.util.*;

// Class name
public class FootballScores
          BufferedReader keyboard = new BufferedReader(new InputStreamReader(;
          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.
Question by:computerese
    LVL 86

    Expert Comment

    What format would this roster take?

    Author Comment


    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?
    LVL 86

    Accepted Solution

    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(;
    while (s.hasNext()) {
        String player =;

    Author Comment

    @ 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.
    LVL 86

    Expert Comment

    >>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
    LVL 92

    Assisted Solution

    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
    LVL 30

    Expert Comment

    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).
    LVL 86

    Expert Comment


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now