0

I'm supposed to write a code in two different classes, and create different constructors of members with information on their date of birth, name, gender, etc, which are all user inputted. Problem is, an example asks for it to be written like this:

Input the team members (Name:Surname:Sex:DD/MM/YYYY):

But as this will come out a string, how am I supposed to change some of these into integers when recording different dates of birth, etc? As my current constructer is :

public Member(String name, String surname, int date, int month, int year, char sex){

How can I differentiate the String inputted, from name, date, year, etc?

public class Member {

    private String name;
    private String surname;
    private int date;
    private int month;
    private int year;
    private char sex;

     public Member(String name, String surname, int date, int month, int year, char sex){
         this.name = name;    
         this.surname = surname;
         this.date = date;
         this.month = month;
         this.year = year;
         this.sex = sex;
     }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public int getDate() {
        return date;
    }

    public int getMonth() {
        return month;
    }

    public int getYear() {
        return year;
    }

    public char getSex() {
        return sex;
    }
}

Other class runs on these methods. I haven't developed the other one yet.

khelwood
  • 55,782
  • 14
  • 81
  • 108
help
  • 19
  • 4

1 Answers1

2

Before creating a new Member, you need to deal with the input.

EDITED Conforming suggested by Gunnar, first verify if the input is the way expected. One way to do that is by creating a Pattern and checking if it matches with the input. There's a lot of material about that (example).

After the input verification, you can slip the input and extract what you want. Like this:

String input = "Name:Surname:Sex:DD/MM/YYYY";
String[] parts = input.split(":");

System.out.println(parts[0]); // It will print: Name
System.out.println(parts[1]); // It will print: Surname
System.out.println(parts[2]); // It will print: Sex
System.out.println(parts[3]); // It will print: DD/MM/YYYY

If you want to split the date too, just use:

String[] date = parts[3].split("/");

System.out.println(date[0]); // It will print: DD
System.out.println(date[1]); // It will print: MM
System.out.println(date[2]); // It will print: YYYY

Notice that all elements are String. So you will have to convert Sex to char.

char sex = parts[2].charAt(0);

And all elements from date to int.

int day = Integer.parseInt(date[0]);

And do the same for month and year.

Karla
  • 96
  • 1
  • 1
  • 6
  • I suggest adding some parsing and validation to the input. For example, before accessing the array you need to check whether the array contains the elements you are expecting. Next would be validation of the strings. For date you can also use a SimpleDateFormat to parse the values. – Gunnar Oct 22 '19 at 18:27
  • Thanks Gunnar! I agree with you. I edited my answer. – Karla Oct 22 '19 at 23:49