1 /* 2 * This program will do a bit of character string processing. 3 */ 4 5 package stringthing; 6 7 public class StringThing { 8 9 public static void main(String[] args) { 10 // POINT A: CREATE A PRINT SOME STRINGS THAT REPRESENT NAMES 11 String singer = "Holiday, Billie"; 12 String sculptor = "Claudel, Camille"; 13 String painter = "Picasso, Pablo"; 14 String dancer = "Zotto, Osvaldo"; 15 String self = "Scalzo, Kyle"; 16 System.out.println("\nNames ..."); 17 System.out.println(singer); 18 System.out.println(sculptor); 19 System.out.println(painter); 20 System.out.println(dancer); 21 System.out.println(self); 22 23 // POINT B: COMPUTE AND PRINT THE LENGTHS OF THE STRINGS, WITHOUT LABELS 24 int singerLength = singer.length(); 25 int sculptorLength = sculptor.length(); 26 int painterLength = painter.length(); 27 int dancerLength = dancer.length(); 28 int selfLength = self.length(); 29 System.out.println("\nName Lengths ..."); 30 System.out.println(singerLength); 31 System.out.println(sculptorLength); 32 System.out.println(painterLength); 33 System.out.println(dancerLength); 34 System.out.println(selfLength); 35 36 // POINT C: COMPUTE AND PRINT THE LOCATION OF THE COMMA WITHIN EACH STRING, NO LABELS 37 int singerCommaPosition = singer.indexOf(","); 38 int sculptorCommaPosition = sculptor.indexOf(","); 39 int painterCommaPosition = painter.indexOf(","); 40 int dancerCommaPosition = dancer.indexOf(","); 41 int selfCommaPosition = self.indexOf(","); 42 System.out.println("\nComma positions ..."); 43 System.out.println(singerCommaPosition); 44 System.out.println(sculptorCommaPosition); 45 System.out.println(painterCommaPosition); 46 System.out.println(dancerCommaPosition); 47 System.out.println(selfCommaPosition); 48 49 // POINT D: COMPUTE AND PRINT THE FIVE FIRST NAMES, WITH NO LABELS 50 String singerFirst = singer.substring(9); 51 String sculptorFirst = sculptor.substring(9); 52 String painterFirst = painter.substring(9); 53 String dancerFirst = dancer.substring(7); 54 String selfFirst = self.substring(8); 55 System.out.println("\nFirst names ..."); 56 System.out.println(singerFirst); 57 System.out.println(sculptorFirst); 58 System.out.println(painterFirst); 59 System.out.println(dancerFirst); 60 System.out.println(selfFirst); 61 62 // POINT E: COMPUTE AND PRINT THE FIVE LAST NAMES, WITH NO LABELS 63 String singerLast = singer.substring(0,7); 64 String sculptorLast = sculptor.substring(0,7); 65 String painterLast = painter.substring(0,7); 66 String dancerLast = dancer.substring(0,5); 67 String selfLast = self.substring(0,6); 68 System.out.println("\nLast names ..."); 69 System.out.println(singerLast); 70 System.out.println(sculptorLast); 71 System.out.println(painterLast); 72 System.out.println(dancerLast); 73 System.out.println(selfLast); 74 75 // POINT F: COMPUTE AND PRINT THE FIRST NAMES, AGAIN 76 System.out.println("\nFirst names, once again ..."); 77 System.out.println(firstName(singer)); 78 System.out.println(firstName(sculptor)); 79 System.out.println(firstName(painter)); 80 System.out.println(firstName(dancer)); 81 System.out.println(firstName(self)); 82 83 // POINT G: COMPUTE AND PRINT THE LAST NAMES, AGAIN 84 System.out.println("\nLast names, once again ..."); 85 System.out.println(lastName(singer)); 86 System.out.println(lastName(sculptor)); 87 System.out.println(lastName(painter)); 88 System.out.println(lastName(dancer)); 89 System.out.println(lastName(self)); 90 91 // POINT H: COMPUTE AND PRINT THE FULL NAMES, NATURAL STYLE 92 System.out.println("\nFull names, natural style ..."); 93 System.out.println(fullName(singer)); 94 System.out.println(fullName(sculptor)); 95 System.out.println(fullName(painter)); 96 System.out.println(fullName(dancer)); 97 System.out.println(fullName(self)); 98 99 } 100 101 private static String fullName(String dsn) { 102 int comma = dsn.indexOf(","); 103 int spaceIndex = dsn.indexOf(" "); 104 String first = dsn.substring(spaceIndex); 105 String last = dsn.substring(0,comma); 106 return first + " " + last; 107 108 } 109 110 private static String lastName(String directoryStyleName) { 111 int comma = directoryStyleName.indexOf(","); 112 return directoryStyleName.substring(0,comma); 113 } 114 115 private static String firstName(String directoryStyleName) { 116 int spaceIndex = directoryStyleName.indexOf(" "); 117 return directoryStyleName.substring(spaceIndex + 1); 118 } 119 } 120