-
Notifications
You must be signed in to change notification settings - Fork 0
/
KennedySpeech
264 lines (222 loc) · 6.8 KB
/
KennedySpeech
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
////-------------------------------------------------------------------
// Programmer: Amadou Barry
// Course: COSC 1336 Sections 011/015
// Semester: Spring 2019
// Assignment#: Semester Project
// Due Date: April 24, 2019 @ 11:59 pm
////-------------------------------------------------------------------
package semesterProject;
//for GUIs using swing
//import classes needed
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.io.*;
import java.lang.Object;
public class KennedySpeech {
/** * @param args */
public static void main(String[] args)
throws FileNotFoundException{
//declare variables to be used
//in this program
int max;
String output;
File f = new File("OriginalSpeech.txt");
File f2 = new File("FormattedSpeech.txt");
File f3 = new File("FormattedSpeech.txt");
Scanner keyboard = new Scanner(System.in);
Scanner inputFile = new Scanner(f);
Scanner inputFile2 = new Scanner(f2);
PrintStream outputFile = new PrintStream(f3);
// print out absolute path name of file
// that holds the unformatted speech
displayAbsolutePath(f);
// print out absolute path name of file
// that holds the unformatted speech
// invoke method 'getNumCharacters' to get
// the number of characters from the user
max = getNumCharacters();
// print out title for file that will
// hold the formatted speech
output = "\nPresident's Kennedy's inaugural speech " +
"with " + max + " characters per line:";
outputFile.println(output);
outputFile.println();
// invoke method 'readFromfile' to read unformatted
// speech and then format it
readFromFile(inputFile, outputFile, max);
// close files
closeFile(inputFile);
closeFile(outputFile);
// print out formatted speech by invoking
// method 'printFormattedSpeech'
printFormattedSpeech(inputFile2);
closeFile(inputFile2);
// build up string for final output
output = "This program was written by Amadou Barry." +
"\nEnd of program";
// print final 'output' string by
// invoking the method 'printString'
displayString(output);
} // end method main
public static int getNumCharacters()
{
String numCharactersText;
int numCharacters;
// prompt the user for the number of characters
// on each line that the formatted speech will
// have, then read it in
numCharactersText = JOptionPane.showInputDialog(null,
"how many characters per line would you like?");
numCharacters = Integer.parseInt(numCharactersText);
// return this number
return numCharacters;
}// end method getNumCharacters
public static void displayAbsolutePath(File f)
{
// print out absolute path of input file
JOptionPane.showMessageDialog(null,
"The absolute path of the original " +
"is: \n" +
f.getAbsolutePath(),
"Absolute path of original file",
JOptionPane.INFORMATION_MESSAGE);
}// end method printAbsolutePathpublic
public static void printAbsolutePath(File f)
{
// print out absolute path of input file
}// end method printAbsolutePathpublic
static void readFromFile(Scanner inputFile, PrintStream outputFile, int max)
{
String word;
int wordCount, lineCount = 0;
boolean answer;
// using a while loop, go through each 'word'
// of the unformatted speech and place it in its
// appropriate place in the formatted file
while (inputFile.hasNext())
{
//get next word from file and
// obtain its length
word = inputFile.next();
wordCount = word.length();
//invoke the method 'checkIfParagraphNumber'
// to see if this current 'word' is a
// paragraph number
answer = checkIfParagraphNumber(word);
// it s a paragraph number
if (answer)
{
// advance two new lines for new
// paragraph and change value of
// 'lineCount' to start again
// at zero
outputFile.println();
outputFile.println();
lineCount = 0;
} // end if statement
// not a paragraph
if (answer == false)
{
// we will go beyond the number of
// characters on this current line
if ((lineCount + wordCount + 1) >= max)
{
//start new 'lineCount'
lineCount = 0;
// update new value 'lineCount'
// that includes the length of this
// word plus one (for a space)
lineCount = lineCount + wordCount + 1;
// print out this word to
// the formatted file
outputFile.println();
outputFile.print(word + " ");
} // end if statement
else // can safely continue printing this
{ // word on the current line (i.e. it
// fits on this line without
// exceeding 'max' characters)
outputFile.print(word + " ");
//update new value of 'lineCount'
// that includes the length of this
// word plus one (for space)
lineCount = lineCount + wordCount + 1;
} // end else statement
} //end outer if statement
} // end while loop
} // end method readFromFile
public static boolean checkIfParagraphNumber(String word)
{
char aChar;
int i, n;
boolean answer;
// get the length of this current 'word'
n = word.length();
i = 0;
// using a while loop, scan through each
// character of 'word' to see if it is a number
while (i < n)
{
aChar = word.charAt(i);
switch(aChar)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': answer = true; break;
default: answer = false; //not a number
} // end switch statement
i++;
} // while loop
// get last character in this string to test
// to see if it is a paragraph number
aChar = word.charAt(n - 1);
// check to see if this is really
// a paragraph number
if (aChar == '#') // yes it is
{
answer = true;
}
else //not a paragraph number
{
answer = false;
}
return answer;
}// end method checkIfParagraphNumber
public static void printFormattedSpeech(Scanner inputFile2)
{
String line;
// using a while loop, read each line from
// the newly created file that holds the
// formatted speech and print it to the screen
while (inputFile2.hasNext())
{
line = inputFile2.nextLine();
System.out.println(line);
}
}// end printOutFormattedSpeech
public static void closeFile(Scanner fileName)
{
// close file
fileName.close();
}// end closeFile
public static void closeFile(PrintStream fileName)
{
// close file
fileName.close();
}// end closeFile (overloaded)
public static void displayString(String outString)
{
// print out the formal parameter 'outString'
JOptionPane.showMessageDialog(null,
outString,
"Good bye!",
JOptionPane.INFORMATION_MESSAGE);
}// end method displayString
}// end class KennedySpeech