Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nameformatter;
- public class NameFormatter {
- private String unformedName;
- private String formedName;
- private String nameA;
- private String nameB;
- private String nameC;
- public NameFormatter(String name) {
- this.unformedName = "Unknown";
- this.formedName = "Unknown";
- this.nameA = "";
- this.nameB = "";
- this.nameC = "";
- this.setName(name);
- this.formatName();
- }
- private void setName(String name) {
- this.unformedName = name;
- }
- public String formatName() {
- // The below block catches middle initial
- if (this.unformedName.length() == 1) {
- this.formedName = this.unformedName;
- return this.formedName;
- }
- // This check ensures we capture instances of multiple first name
- try {
- String[] names = this.unformedName.split("\\s+");
- if (names.length == 2) {
- this.nameA = names[0].substring(0, 1) +
- this.nameA.substring(1).toLowerCase();
- this.nameB = names[1].substring(0, 1) +
- this.nameB.substring(1).toLowerCase();
- this.formedName = this.nameA + " " + this.nameB;
- return this.formedName;
- }
- else if (names.length == 3) {
- this.nameA = names[0].substring(0, 1) +
- this.nameA.substring(1).toLowerCase();
- this.nameB = names[1].substring(0, 1) +
- this.nameB.substring(1).toLowerCase();
- this.nameC = names[2].substring(0, 1) +
- this.nameC.substring(1).toLowerCase();
- this.formedName = nameA + " " + nameB + " " + nameC;
- return this.formedName;
- }
- else {
- this.formedName = this.unformedName.substring(0, 1) +
- this.unformedName.substring(1).toLowerCase();
- return this.formedName;
- }
- }
- catch(Exception e) {
- this.formedName = this.unformedName.substring(0, 1) +
- this.unformedName.substring(1).toLowerCase();
- return this.formedName;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement