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