/* * This program will do a bit of character string processing. */ package stringthing; public class StringThing { public static void main (String [] args ){ // POINT A: CREATE A PRINT SOME STRING THAT REPRESENT NAMES String singer = "Holiday, Billie"; String sculptor = "Claudel, Camille"; String painter = "Picasso, Pablo"; String dancer = "Zotto, Osvaldo"; String self = "Soe, Myat Min"; System.out.println("\nNames ..."); System.out.println(singer); System.out.println(sculptor); System.out.println(painter); System.out.println(dancer); System.out.println(self); //POINT B: COMPUTE AND PRINT THE LENGTHS OF THE STRINGS, WITHOUT LABELS int singerLength = singer.length(); int sculptorLength = sculptor.length(); int painterLength = painter.length(); int dancerLength = dancer.length(); int selfLength = self.length(); System.out.println("\nName Lengths ..."); System.out.println(singerLength); System.out.println(sculptorLength); System.out.println(painterLength); System.out.println(dancerLength); System.out.println(selfLength); //POINT C: COMPUTE AND PRINT THE LOCATION OF THE COMMA WITHIN EACH STRING, NO LABELS int singerComma = singer.indexOf(","); int sculptorComma = sculptor.indexOf(","); int painterComma = painter.indexOf(","); int dancerComma = dancer.indexOf(","); int selfComma = self.indexOf(","); System.out.println("\nComma positions ..."); System.out.println(singerComma); System.out.println(sculptorComma); System.out.println(painterComma); System.out.println(dancerComma); System.out.println(selfComma); //POINT D: COMPUTE AND PRINT THE FIVE FIRST NAMES, WITH NO LABELS String singerFirst = singer.substring(singerComma+2); String sculptorFirst = sculptor.substring(painterComma+2); String painterFirst = painter.substring(painterComma+2); String dancerFirst = dancer.substring(dancerComma+2); String selfFirst = self.substring(selfComma+2); System.out.println("\nFirst names ..."); System.out.println(singerFirst); System.out.println(sculptorFirst); System.out.println(painterFirst); System.out.println(dancerFirst); System.out.println(selfFirst); //POINT E: COMPUTE AND PRINT THE FIVE LAST NAMES, WITH NO LABELS String singerLast = singer.substring(0,singerComma); String sculptorLast = sculptor.substring(0,sculptorComma); String painterLast = painter.substring(0,painterComma); String dancerLast = dancer.substring(0,dancerComma); String selfLast = self.substring(0,selfComma); System.out.println("\nLast names ..."); System.out.println(singerLast); System.out.println(sculptorLast); System.out.println(painterLast); System.out.println(dancerLast); System.out.println(selfLast); //POINT F: COMPUTE AND PRINT THE FIRST NAMES, AGAIN System.out.println("\nFirst names, once again ..."); System.out.println(firstName(singer)); System.out.println(firstName(sculptor)); System.out.println(firstName(painter)); System.out.println(firstName(dancer)); System.out.println(firstName(self)); //POINT G: COMPUTE AND PRINT THE LAST NAMES, AGAIN System.out.println("\nLast names, once again ..."); System.out.println(lastName(singer)); System.out.println(lastName(sculptor)); System.out.println(lastName(painter)); System.out.println(lastName(dancer)); System.out.println(lastName(self)); //POINT H: COMPUTE AND PRINT THE FULL NAMES, NATURAL STYLE System.out.println("\nFull names, natural style ..."); System.out.println(fullName(singer)); System.out.println(fullName(sculptor)); System.out.println(fullName(painter)); System.out.println(fullName(dancer)); System.out.println(fullName(self)); } private static String fullName(String dsn) { return firstName(dsn) + " " + lastName(dsn); } private static String lastName(String directoryStyleName) { int lastNameComma = directoryStyleName.indexOf(","); String lastName = directoryStyleName.substring(0,lastNameComma); return lastName; } private static String firstName(String directoryStyleName) { int firstNameComma = directoryStyleName.indexOf(","); String firstName = directoryStyleName.substring(firstNameComma+2); return firstName; } }