1 /* 2 * 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, Ozvaldo"; 15 String self = "Tepfenhart, Emily"; 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 LENGTH 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("\nNames 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 37 //POINT C: COMPUTE AND PRINT THE LOCATION OF THE COMMA WITHIN EACH STRING, NO LABELS 38 int singerCommaPosition = singer.indexOf(","); 39 int sculptorCommaPosition = sculptor.indexOf(","); 40 int painterCommaPosition = painter.indexOf(","); 41 int dancerCommaPosition = dancer.indexOf(","); 42 int selfCommaPosition = self.indexOf(","); 43 System.out.println("\nComma positions ..."); 44 System.out.println(singerCommaPosition); 45 System.out.println(sculptorCommaPosition); 46 System.out.println(painterCommaPosition); 47 System.out.println(dancerCommaPosition); 48 System.out.println(selfCommaPosition); 49 50 //POINT D: COMPUTE AND PRINT THE FIVE FIRST NAMES, WITH NO LABELS 51 String singerFirst = singer.substring(9); 52 String sculptorFirst = sculptor.substring(9); 53 String painterFirst = painter.substring(9); 54 String dancerFirst = dancer.substring(7); 55 String selfFirst = self.substring(12); 56 System.out.println("\nFirst names ..."); 57 System.out.println(singerFirst); 58 System.out.println(sculptorFirst); 59 System.out.println(painterFirst); 60 System.out.println(dancerFirst); 61 System.out.println(selfFirst); 62 63 //POINT E: COMPUTE AND PRINT THE FIVE LAST NAMES, WITH NO LABELS 64 String singerLast = singer.substring(0,7); 65 String sculptorLast = sculptor.substring(0,7); 66 String painterLast = painter.substring(0,7); 67 String dancerLast = dancer.substring(0,5); 68 String selfLast = self.substring(0,10); 69 System.out.println("\nLast names ..."); 70 System.out.println(singerLast); 71 System.out.println(sculptorLast); 72 System.out.println(painterLast); 73 System.out.println(dancerLast); 74 System.out.println(selfLast); 75 76 //POINT F: COMPUTE AND PRINT THE FIRST FIVE NAMES, AGAIN 77 System.out.println("\nFirst names, once again ..."); 78 System.out.println(firstName(singer)); 79 System.out.println(firstName(sculptor)); 80 System.out.println(firstName(painter)); 81 System.out.println(firstName(dancer)); 82 System.out.println(firstName(self)); 83 84 //POINT G: COMPUTE AND PRINT THE LAST FIVE NAMES, AGAIN 85 System.out.println("\nLast names, once again ..."); 86 System.out.println(lastName(singer)); 87 System.out.println(lastName(sculptor)); 88 System.out.println(lastName(painter)); 89 System.out.println(lastName(dancer)); 90 System.out.println(lastName(self)); 91 92 //POINT H: COMPUTE AND PRINT THE FULL NAMES, NATURAL STYLE 93 System.out.println("\nFull names, natural style ..."); 94 System.out.println(fullName(singer)); 95 System.out.println(fullName(sculptor)); 96 System.out.println(fullName(painter)); 97 System.out.println(fullName(dancer)); 98 System.out.println(fullName(self)); 99 100 } 101 102 private static String firstName(String directoryStyleNames) { 103 //return substring, starting with the first letter of the firstName, which comes after the space 104 105 return directoryStyleNames.substring(directoryStyleNames.indexOf(" ") + 1); 106 } 107 108 public static String lastName(String directoryStyleName){ 109 //return substring, starting with the first letter of the lastName, which is the 0st letter, up to the 110 // last letter before the comma 111 112 113 return directoryStyleName.substring(0,directoryStyleName.indexOf(",")); 114 } 115 116 public static String fullName(String dsn){ 117 //return full name in natural style, in which firstName comes first and last name comes next, parted by space 118 //methods for firstName and lastName created as part of point f and point g 119 120 return firstName(dsn) + " " + lastName(dsn); 121 } 122 123 124 } 125 126