StringThing.java
1    /* 
2     * This program will do a bit of 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, Osvaldo";
15           String self = "Borges, Isaiah";
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 LENGTHS 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           //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           System.out.println("\nComma positions ...");
43           System.out.println(singerCommaPosition);
44           System.out.println(sculptorCommaPosition);
45           System.out.println(painterCommaPosition);
46           System.out.println(dancerCommaPosition);
47           System.out.println(selfCommaPosition);
48   
49           //POINT D : COMPUTE AND PRINT THE FIVE FIRST NAMES, WITH NO LABELS
50           String singerFirst = singer.substring(9);
51           String sculptorFirst = sculptor.substring(9);
52           String painterFirst = painter.substring(9);
53           String dancerFirst = dancer.substring(7);
54           String selfFirst = self.substring(8);
55           System.out.println("\nFirst names ...");
56           System.out.println(singerFirst);
57           System.out.println(sculptorFirst);
58           System.out.println(painterFirst);
59           System.out.println(dancerFirst);
60           System.out.println(selfFirst);
61           //POINT E : COMPUTE AND PRINT THE FIVE LAST NAMES, WITH NO LABELS
62           String singerLast = singer.substring(0, 7);
63           String sculptorLast = sculptor.substring(0, 7);
64           String painterLast = painter.substring(0, 7);
65           String dancerLast = dancer.substring(0, 5);
66           String selfLast = self.substring(0, 6);
67           System.out.println("\nLast names ...");
68           System.out.println(singerLast);
69           System.out.println(sculptorLast);
70           System.out.println(painterLast);
71           System.out.println(dancerLast);
72           System.out.println(selfLast);
73   
74           //POINT F : COMPUTE AND PRINT THE FIRST NAMES, AGAIN
75           System.out.println("\nFirst names, once again ...");
76           System.out.println(firstName(singer));
77           System.out.println(firstName(sculptor));
78           System.out.println(firstName(painter));
79           System.out.println(firstName(dancer));
80           System.out.println(firstName(self));
81   
82   
83           //POINT G : COMPUTE AND PRINT THE LAST NAMES, AGAIN
84           System.out.println("\nLast names, once again ...");
85           System.out.println(lastName(singer));
86           System.out.println(lastName(sculptor));
87           System.out.println(lastName(painter));
88           System.out.println(lastName(dancer));
89           System.out.println(lastName(self));
90           //POINT H : COMPUTE AND PRINT THE FULL NAMES, NATURAL STYLE
91   
92           System.out.println("\nFull names, natural style ...");
93           System.out.println(fullName(singer));
94           System.out.println(fullName(sculptor));
95           System.out.println(fullName(painter));
96           System.out.println(fullName(dancer));
97           System.out.println(fullName(self));
98       }
99   
100      private static String fullName(String dsn) {
101          return firstName(dsn) + " " + lastName(dsn) ;
102      }
103  
104      private static String lastName(String directoryStyleName) {
105          int comma = directoryStyleName.indexOf(",");
106          return directoryStyleName.substring(0, comma) ;
107      }
108  
109      private static String firstName(String directoryStyleName) {
110          int space = directoryStyleName.indexOf(" ");
111          return directoryStyleName.substring(space + 1);
112      }
113  
114  }