At the entrance of my program receives data in this format: -u id name sex bd . id - number, name - name, sex - sex, bd - birthday.

I have a Person class in which all these values ​​are added:

 public class Person { private String name; private Sex sex; private Date birthDay; private Person(String name, Sex sex, Date birthDay) { this.name = name; this.sex = sex; this.birthDay = birthDay; } public static Person createMale(String name, Date birthDay){ return new Person(name, Sex.MALE, birthDay); } public static Person createFemale(String name, Date birthDay){ return new Person(name, Sex.FEMALE, birthDay); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Sex getSex() { return sex; } public void setSex(Sex sex) { this.sex = sex; } public Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; } } 

There is such a class Sex :

 public enum Sex { MALE, FEMALE } 

How do I pass all the parameters from args to the constructor? This is how this is understandably not working:

 if (args[0].equals("-c")) { allPeople.add(new Person(args[1], Integer.valueOf(args[2]), args[3])); } 
  • Supplement the question with an example of command line arguments passed. In what format do you transmit sex and bd? - GreyGoblin
  • If the problem is with the Date field, use SimpleDateFormat to convert from String to Date. If the problem is with ENUM - make a method that, by the value of the String parameter, will return the desired Enum. If you intentionally decided to close the constructor (and you have it private), then check the value of the argument meaning sex (sex) and call your methods createMale / createFemale - Chubatiy

3 answers 3

Each element of the args array passed to the main method is of type String.

Line

 new Person(args[1], Integer.valueOf(args[2]), args[3]) 

It does not make sense at all - it does not have a Person constructor (String, Integer, String). The constructor is private, and it is clear from the code that it was planned to create instances through the createMale and createFemale factory methods.

You must explicitly create objects of the desired type. Sex - depending on the value of the parameter (better - through the conditional expression, but it is possible also through Enum.valueOf ()), the date - through SimpleDateFormat.

I would like to see the call line completely, because it’s not clear where the call comes from

 if (args[0].equals("-c")) 

when written

At the entrance of my program data is received in this format: -u id name sex bd

    Args: -c name sex bd

    Constructor:

     public Person(String name, Sex sex, int birthDay) {...} 

    Main:

     if (args[0].equals("-c")) { Sex sex = Sex.MALE.name().equals(args[2]) ? Sex.MALE : Sex.FEMALE; allPeople.add(new Person(args[1], sex, Integer.valueOf(args[3]))); } 
    • A sex type field needs to be received like this: Sex.valueOf(args[2]) - Leonid Lunin
     //предположу, что пол передается как male/female //а дата как dd/MM/yyyy, тогда, опуская большую часть проверок DateFormat format = new SimpleDateFormat("dd/MM/yyyy"); Date date = format.parse(args[3]); if (args[2].equals("male")) { allPeople.add(Person.createMale(args[1], date)); } else { allPeople.add(Person.createFemale(args[1], date)); }