user15082428
user15082428

Reputation:

Splitting the values of the returned value of toString

I I need to split the values that toString method returns. The format looks like this:

Employee{id='334845', name='Yadiel Stefano Olan Cunanan', department='Human Resource', gender=M, 
age=29, employment status=Permanent, salary=29066.0, years of service=5}
Employee{id='227800', name='Yadira Mariam Talatala Esca�o', department='Marketing', gender=F, age=39, 
employment status=Permanent, salary=23533.0, years of service=11}

We were asked not to touch the Employee class which encapsulates all the variables that the main activity utilizes. I wanted to split the values such that I will only get the values in the ' ' (i.e. 334845, Yadiel Stefano Olan Culanan, etc.).

I tried the .split(",") and (",(?=([^"]"[^"]")[^"]$)"), but I get an error. Can you help me with what should I put as the regex value. Thank you in advance!

Upvotes: 1

Views: 388

Answers (3)

RoundedHouse
RoundedHouse

Reputation: 81

The toString returns an employee String, from the output you've presented. Thus, to get each individual field, you could do this:

Your class would probably be in this format:

class Employee{
  private int id;
  private String name;
  private String department;
  private String gender; or private GenderCategory gender as an enum
  private int age;
  private String employmentStatus; or private EmploymentCategory employeeStatus as an 
                             enum
  private BigDecimal salary or private double salary
  private int yearsofService;

  public Employee(){
      //no args constructor
  }

  public Employee(all argsConstructor){
  }

  //and you could implement builder pattern here if you want
  
  //your toString function() here
  public toString(){
     return "Employee{id=" + id + ", name=" + name
     + ", department=" + department
            + ", gender=" + gender+ ", age=" + age+ ", employmentStatus=" + 
                  employmentStatus+ ", salary=" + salary, yearsofService=" + 
                    yearsofService}";
  }
}

//main Class accessing employeeClass Here
 Employee emp = new Employee()
 for(Employee employees : emp){
    //perform your logics of finding a value or splitting any character youwant
    //using **indexOf, charAt, subString()** and fxn you think can solve your problem
    eg: System.out.println(employees.toString().split(",")
 }

Upvotes: -1

Anuj
Anuj

Reputation: 1665

Your problem can be solved using regex. The regex required is- ='?(.+?)'?[,}].


Code

String[] input =
    {
        "Employee{id='334845', name='Yadiel Stefano Olan Cunanan', department='Human Resource', gender=M, age=29, employment status=Permanent, salary=29066.0, years of service=5}",
        "Employee{id='227800', name='Yadira Mariam Talatala Esca�o', department='Marketing', gender=F, age=39, employment status=Permanent, salary=23533.0, years of service=11}"
    };

//Set the pattern
Pattern pattern = Pattern.compile("='?(.+?)'?[,}]");
ArrayList<ArrayList<String>> values = new ArrayList<>();

//for each input
for (int i = 0;i < input.length;i++)
{
    //Set the matcher object
    Matcher matcher = pattern.matcher(input[i]);
    //Add a new index to the array list
    values.add(new ArrayList<>());

    //Obtain all the values and add them to the ArrayList
    while (matcher.find()) values.get(i).add(matcher.group(1));
}

//Print each value
for (ArrayList<String> value : values) System.out.println(value);

Output

[334845, Yadiel Stefano Olan Cunanan, Human Resource, M, 29, Permanent, 29066.0, 5]
[227800, Yadira Mariam Talatala Esca�o, Marketing, F, 39, Permanent, 23533.0, 11]

Please note that the regex will only work if the input string is the same pattern as you provided. Any change in the input string pattern may cause the regex to fail and it will require an updating.

Do comment if you need any more help or if you are facing any problem understanding the solution.

Upvotes: 1

Gabriel Belingueres
Gabriel Belingueres

Reputation: 2985

You should split around the ' char, which will return an array of strings where the even indexes will give you the string before the opening ', and the odd indexes will give you the string inside the '':

String[] arr = yourString.split("'");
for(int i = 1; i < arr.length; i += 2)
  System.out.println(arr[i]);

Upvotes: 1

Related Questions