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        public static void main(String[] args) {
9            //POINT A: CREATE A. PRINT SOME STRINGS THAT REPRESENT NAMES
10           String singer = "Holiday, Billie";
11           String sculptor = "Claudel, Camille";
12           String painter = "Picasso, Pablo";
13           String dancer = "Zotto, Osvaldo";
14           String self = "Franklin, John";
15   
16           System.out.println("\nNames ...");
17           System.out.println("singer name = " + singer);
18           System.out.println("sculptor name = " + sculptor);
19           System.out.println("painter name = " + painter);
20           System.out.println("dancer name = " + dancer);
21           System.out.println("self = " + 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   
30           System.out.println("\nNames lengths ...");
31           System.out.println(singerLength);
32           System.out.println(sculptorLength);
33           System.out.println(painterLength);
34           System.out.println(dancerLength);
35           System.out.println(selfLength);
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   
44           System.out.println("\n commapositions ...");
45           System.out.println(singerCommaPosition);
46           System.out.println(sculptorCommaPosition);
47           System.out.println(painterCommaPosition);
48           System.out.println(dancerCommaPosition);
49           System.out.println(selfCommaPosition);
50   
51           //POINT D: COMPUTE AND PRINT THE FIVE FIRST NAMES. NO LABELS
52           String singerFirst = "Billie";
53           String sculptorFirst = "Camille";
54           String painterFirst = "Pablo";
55           String dancerFirst = "Osvaldo";
56           String selfFirst = "John";
57   
58           System.out.println("\n First names ...");
59           System.out.println(singerFirst.substring(0, 6));
60           System.out.println(sculptorFirst.substring(0, 6));
61           System.out.println(painterFirst.substring(0, 5));
62           System.out.println(dancerFirst.substring(0, 7));
63           System.out.println(selfFirst.substring(0, 4));
64   
65           //POINT E: COMPUTE AND PRINT THE FIVE LAST NAMES, WITH NO LABELS
66           String singerLast = "Holiday";
67           String sculptorLast = "Camille";
68           String painterLast = "Picasso";
69           String dancerLast = "Zotto";
70           String selfLast = "Franklin";
71   
72           System.out.println("\nLast Names ...");
73           System.out.println(singerLast.substring(0, 7));
74           System.out.println(sculptorLast.substring(0, 7));
75           System.out.println(painterLast.substring(0, 7));
76           System.out.println(dancerLast.substring(0, 5));
77           System.out.println(selfLast.substring(0, 8));
78   
79   
80           //POINT F: COMPUTE AND PRINT THE FIRST NAMES, AGAIN
81           System.out.println("\nFirst names, once again ...");
82           System.out.println(firstName(singer));
83           System.out.println(firstName(sculptor));
84           System.out.println(firstName(painter));
85           System.out.println(firstName(dancer));
86           System.out.println(firstName(self));
87   
88           //POINT G: COMPUTE AND PRINT THE LAST NAMES, AGAIN
89                   System.out.println("\nLast names, once again ...");
90                   System.out.println(lastName(singer));
91                   System.out.println(lastName(sculptor));
92                   System.out.println(lastName(painter));
93                   System.out.println(lastName(dancer));
94                   System.out.println(lastName(self));
95   
96           //POINT H: COMPUTE AND PRINT THE FULL NAMES, NATURAL STYLE
97           System.out.println("\nFull names, natural style ...");
98           System.out.println(fullName(singer));
99           System.out.println(fullName(sculptor));
100          System.out.println(fullName(painter));
101          System.out.println(fullName(dancer));
102          System.out.println(fullName(self));
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149      }
150  
151      private static String fullName(String dsn) {
152          return firstName(dsn)+ " " + lastName(dsn);
153      }
154  
155      private static String lastName(String directoryStyleName) {
156  
157           return(directoryStyleName.substring(0,directoryStyleName.indexOf(",")));
158  
159  
160      }
161  
162  
163  
164  
165      private static String firstName(String directoryStyleName) {
166  
167  
168  
169          return(directoryStyleName.substring(directoryStyleName.lastIndexOf(" ")));
170      }
171  
172  }