This article main focus is to show a concrete example in parsing a String text delimited with pipe character |. The input is a string delimited with | and the expected out is an object Employee. The employee class holds information such as name, age, location, birthdate and status. Example only uses pipe as delimiter however the character can be easily replaced to suite your business requirements.

Main objectives of this example is to show how to parse a string delimiter input into a more sensible information. This example also shows encapsulation which is the use of the setters and getters. Moreover example also shows the usage of java constructors and how to instantiate a new object.

Instructions in parsing a pipe delimited string in Java

Given a string deimited input with format name|age|status|birthdate|location. Create a java object Employee that accepts the string input and would be able to parse it and convert into fields using setters and getters. Print the information such as name, age, location, birthdate and status by accessing the method of the employee class.

Java API used in this example

StringTokenizer – an object under java.util package.

  • hasMoreTokens() – it returns true if there are more tokens available and false if otherwise
  • nextToken() – returns the next token of the StringTokenizer object

Java source code that converts a pipe delimited string into Employee object

Main Class

package com.javatutorialhq.tutorial;

import com.javatutorialhq.tutorial.utilities.Employee;

/**
* This sample source code shows
* how to parse a pipe delimited string to an object
* This is the Main class
*/

public class StringDelimited {

public static void main(String[] args) {

 String sampleString = "anne curtis|17|Single|January 12, 1989|manila";
 Employee emp = new Employee(sampleString);
 System.out.println("Employee name: "+emp.getName());
 System.out.println("Employee age: "+emp.getAge());
 System.out.println("Employee location: "+emp.getLocation());
 }

}

Employee Class

package com.javatutorialhq.tutorial.utilities;

/**
* This sample source code shows
* how to parse a pipe delimited string to an object
* This is the object template
*/

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Employee {
 private String name;
 private int age;
 private String birthdate;
 private String status;
 private String location;
 public Employee(){

 }
 public Employee(String information) {
 super();
 List alist = new ArrayList();
 StringTokenizer st = new StringTokenizer(information, "|");
 while(st.hasMoreTokens()){
 alist.add(st.nextToken());
 }
 this.setName(alist.get(0));
 this.setAge(Integer.parseInt(alist.get(1)));
 this.setBirthdate(alist.get(3));
 this.setStatus(alist.get(2));
 this.setLocation(alist.get(4));
 }

public void setName(String name){
 this.name = name.toUpperCase();
 }
 public String getName(){
 return this.name;
 }
 public String getBirthdate() {
 return birthdate;
 }
 public void setBirthdate(String birthdate) {
 this.birthdate = birthdate;
 }
 public String getLocation() {
 return location;
 }
 public void setLocation(String location) {
 this.location = location;
 }
 public String getStatus() {
 return status;
 }
 public void setStatus(String status) {
 this.status = status;
 }
 public int getAge() {
 return age;
 }
 public void setAge(int age) {
 this.age = age;
 }

}

Sample output:

Employee name: ANNE CURTIS
Employee age: 17
Employee location: manila

Suggested Reading List: