Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 524
  • Last Modified:

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.
2 Solutions
What format would this roster take?
computereseAuthor Commented:

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?
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();
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

computereseAuthor Commented:

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.
>>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
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

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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now