In: Computer Science
In Java, please create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines.
** Each method below, including main, should handle (catch) any
Exceptions that are thrown. ** ** If an Exception is thrown and
caught, print the Exception's message to the command line.
**
thanks for the question, here are the all methods written in Java.
======================================================================
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class CheckString {
public static void
checkWord(String word) throws Exception {
if
(word == null ||
!Character.isLetter(word.charAt(0))) {
throw new Exception("This is not a
word");
}
}
public static String getWord()
{
Scanner scanner =
new Scanner(System.in);
System.out.print("Enter a word:
");
String word =
"";
word =
scanner.nextLine();
try
{
checkWord(word);
} catch
(Exception e) {
System.out.println(e.getMessage());
}
return
word;
}
public static void
writFile(String[] words, String fileName) {
try
{
FileWriter writer = new
FileWriter(new File(fileName));
for (String word : words) {
writer.write(word +
"\r\n");
}
writer.flush();
writer.close();
} catch
(IOException e) {
System.out.println("Error: Unable
open/write file: " + fileName);
}
}
public static
ArrayList<String> readFile(String fileName) {
ArrayList<String>
words = new ArrayList<>();
try
{
Scanner fileReader = new
Scanner(new File(fileName));
while (fileReader.hasNextLine())
words.add(fileReader.nextLine());
} catch
(FileNotFoundException e) {
System.out.println("Error: Unable to
read/open file: " + fileName);
}
return
words;
}
public static void
main(String[] args) {
String word =
getWord();
System.out.println("You entered:
" + word);
String[] testData =
{"January", "February",
"March"};
String fileName =
"data.txt";
writFile(testData,
fileName);
ArrayList<String>
fileContents = readFile(fileName);
System.out.println("File
Contents:");
for
(String content : fileContents) {
System.out.println(content);
}
}
}
======================================================================