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