1 /* 2 * This program will do a bit of character string processing. 3 */ 4 package stringthing; 5 6 public class StringThing { 7 public static void main(String[] args) { 8 // POINT A: CREATE A PRINT SOME STRINGS THAT REPRESENT NAMES 9 String singer = "Holiday, Billie"; 10 String sculptor = "Claudel, Camille"; 11 String painter = "Picasso, Pablo"; 12 String dancer = "Zotto, Osvaldo"; 13 String self = "Mojumder, Shariar"; 14 15 System.out.println("\nNames . . ."); 16 System.out.println(singer); 17 System.out.println(sculptor); 18 System.out.println(painter); 19 System.out.println(dancer); 20 System.out.println(self); 21 22 // POINT B: COMPUTE AND PRINT THE LENGTHS OF THE STRINGS, WITHOUT LABELS 23 int singerLength = singer.length(); 24 int sculptorLength = sculptor.length(); 25 int painterLength = painter.length(); 26 int dancerLength = dancer.length(); 27 int selfLength = self.length(); 28 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 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(10); 56 57 System.out.println("\nFirst names ..."); 58 System.out.println(singerFirst); 59 System.out.println(sculptorFirst); 60 System.out.println(painterFirst); 61 System.out.println(dancerFirst); 62 System.out.println(selfFirst); 63 64 // POINT E: COMPUTE AND PRINT THE FIVE LAST NAMES, WITH NO LABELS 65 String singerLast=singer.substring(0,7); 66 String sculptorLast=sculptor.substring(0,7); 67 String painterLast=painter.substring(0,7); 68 String dancerLast=dancer.substring(0,5); 69 String selfLast=self.substring(0,8); 70 71 System.out.println("\nLast names ..."); 72 System.out.println(singerLast); 73 System.out.println(sculptorLast); 74 System.out.println(painterLast); 75 System.out.println(dancerLast); 76 System.out.println(selfLast); 77 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 private static String fullName(String dsn) { 104 return firstName(dsn) + " " + lastName(dsn); 105 } 106 private static String lastName(String directoryStyleName) { 107 int IndexOfComma = directoryStyleName.indexOf(","); 108 return directoryStyleName.substring(0,IndexOfComma); 109 } 110 private static String firstName(String directoryStyleName) { 111 int IndexOfSpace = directoryStyleName.indexOf(" "); 112 return directoryStyleName.substring(IndexOfSpace + 1); 113 } 114 }