Creating an Object and initialize it with passing parameters in Java

I am trying to create a new object and initialize it with the parameters which are passed by the following command :

java -jar JAR-FILE.jar store Information.dat ClientName "Address" City Country HomePhone OfficePhone CellPhone

where ClientName is the name of the client , "Address" contains the client's address and etc. The client can have a HomePhone or a OfficePhone or a Cellphone or all of these and even more phone numbers.

here is the class which I tried to initialize it with parameters :

private static void SaveClient(String[] args) throws Exception 
    Client SaveClient = new Client(...);


here is the Client constructor :

public class Client{

private String ClientName;
private Address address;
private List<String> PhoneNumbers;

public Client() {
    this.PhoneNumbers = new ArrayList<String>();

public Client(String ClientName, Address address) {
    this(); = ClientName;
    this.address = address;

public void AddPhoneNumber(String number) {

but I don't know how to initialize it with the passing parameters. We can assume the information is valid, also I don't want to make any changes to the "Client" constructor just modifying the SaveClient


First, you need to determine if the number of supplied arguments meet your expected needs...

// This assumes that address, city, country, home phone, 
// office and cell phone are optional
if (args.length >= 3) {...


// This assumes that all the values are required...
if (args.length >= 9) {...

If either of these cases are untrue (based on your needs), then you should display some kind of error message and probably exit...

You then need to extract the values from args...

// This assumes that some of the arguments are optional...
String clientName = args[2];
String address = args.length > 3 ? args[3] : null;
String city = args.length > 4 ? args[4] : null;
String country = args.length > 5 ? args[5] : null;
String homePhone = args.length > 6 ? args[6] : null;
String officePhone = args.length > 7 ? args[7] : null;
String cellPhone = args.length > 8 ? args[8] : null;


// This assumes that all the parameters are mandatory
String clientName = args[2];
String address = args[3];
String city = args[4];
String country = args[5];
String homePhone = args[6];
String officePhone = args[7];
String cellPhone = args[8];

Then create your Client object...

Client client = new Client(

This of course will mean that your Client object will require a constructor capable of accepting the information your providing. Your example Client won't work, apart from the fact that it has a Person constructor which is invalid, it has no means to accept all this information...

public class Client{

    private String ClientName;
    private Address address;
    private List<String> PhoneNumbers;

    public Client() {
        this.PhoneNumbers = new ArrayList<String>();

    public Client(String clientName, String address, String city, String country, String homePhone, String officePhone, String cellPhone) {
        ClientName = clientName;
        address = new Address(...); // No idea of the parameters for this...

For example...

Use the String[] args, convert these arguments as needed (e.g. to int, long, String, or to whatever you need), and then pass them to the Client constructor.

Assuming your client Class has all the needed properties and the args array elements positioning is known you can go for something like this

Public Client (String[] args){
    this.address = args[6]; // Based on your edit this will need to be an Address object. may look something like: this.address = new Address(args[6]);  assuming Address object has a constructor that accepts String

This can get very ugly if the order of the string array is not correct or things are in the wrong place or not there at all. You will also have to do some casting (converting value types) if the Client property you are trying to save is an int or double or what have you, since the args element is coming as Strings only

Need Your Help

Regular Expression - Two Digit Range (23-79)?


I have been reading the regex questions on this site but my issue seems to be a bit different. I need to match a 2 digit number, such as 23 through 75. I am doing this on an HP-UX Unix system. I...

Documentation on internal variables in ggplot, esp. PANEL

r ggplot2

The answer to this question uses a PANEL variable which seems to be internal to ggplot. But searching the ggplot documentation and also Hadley Wickham's book, I can find no reference to it at all. Is