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