Question
create a new Java application called CheckString (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch)
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. **
Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the first character of the parameter is a letter. If it is not a letter, the method throws an Exception of type Exception with the message of: "This is not a word."
Write a complete Java method called getWord that takes no parameters and returns a String. The method prompts the user for a word, and then calls the checkWord method you wrote in #1 above, passing as a parameter the word the user provided as input. Make sure the getWord method handles the Exception that may be thrown by checkWord.
Write a complete Java method called writeFile that takes two parameters: an array of Strings (arrayToWrite) and a String (filename). The method writes the Strings in the arrayToWrite array to a text file called filename (the parameter), with each String on a separate line.
Write a complete Java method called readFile that takes a String as a parameter (filename) and returns an ArrayList of Strings (fileContents). The method reads the text file identified by the filename parameter and populates the ArrayList with an element for each line in the text file.
In your main method, do the following in the order specified:
Call the getWord method you wrote in #2 above and print the result to the command line.
Create an array of Strings called testData and populate it with at least three elements.
Call the writeFile method you wrote in #3 above passing the array you created in #5.2 and the String "data.txt".
Call the readFile method you wrote in #4 above to read the file you wrote in #5.3. Assign the result of readFile to an ArrayList variable in main called fileContents.
Write a loop to print the contents of the fileContents ArrayList to the command line.
Notes
For this PA you can use any of the IO classes. For example, you can use either FileWriter or PrintWriter, and for reading Strings probably you will want to use BufferedReader. There should be a total of four methods in additional to main: checkWord, getWord (which calls checkWord), writeFile, and readFile. Also, the PA instructions say that the methods including main should handle exceptions. When coding up the solution experiment with try-catch within methods vs having the methods throw an exception and then catching it in main. Be sure to test your code for throwing a FileNotFoundException (it is an easy exception to test).
If you are not sure or want to experiment with different io classes, you can import them all using:
import java.io.*;
There are several implementation approaches. Below is one example framework/outline which might be helpful. Please feel free to experiment with other approaches.
public static void checkWord(String word) throws Exception {
// Uses charAt method to test if the first letter of string variable word
// is a character. If not, throw new exception
}
public static String getWord() {
// Declare a local scanner
// Prompt the user to enter a word
// Think about using a loop to give the user multiple opportunities to correctly enter a string
// Read into a string
// Call checkWord method passing the string as a parameter
// checkWord can throw an exception; call checkWord in a try/catch block
// Return the string to main if a valid string
}
public static void writeFile(String[] arrayToWrite, String filename) throws IOException {
// Example using FileWriter but PrintWriter could be used instead
// Create a FileWriter object
FileWriter fileWordStream = new FileWriter(filename);
// Use a loop to write string elements in arrayToWrite to fileWordStream
// In the loop use the lineSeparator method to put each string on its own line
fileWordStream.write(System.lineSeparator());
}
public static ArrayList readFile(String filename) throws FileNotFoundException, IOException {
// Declare local ArrayList
// Create a new File object using filename parameter
// Check if the file exists, if not throw a new exception
// Create a new BufferedReader object
BufferedReader fileWordStream = new BufferedReader(new FileReader(filename));
// use a loop and the readLine method to read each string (on its own line) from the file
// return the filled ArrayList to main
}
public static void main(String[] args) {
// create a string with literal values to write to the file
String[] testData = {"cat", "dog", "rabbit"};
// Create an ArrayList for reading the file
// Declare a string variable containing the file name data.txt
// Call getWord, assign the returned string to a variable and display it
// Call writeFile and readFile methods in a try block
// Printout the contents of the ArrayList after the call to readFile
// catch two types of exceptions
}
An example run might look like:
run:
Please enter a word: testing
The word is: testing
cat
dog
rabbit
BUILD SUCCESSFUL (total time: 7 seconds)
Here is an example run where an integer is entered instead of a word. A while loop is used to keep the user in the loop until they enter a string:
run:
Please enter a word: 78
This is not a word.
Please enter a word: 59
This is not a word.
Please enter a word: testing
The word is: testing
cat
dog
rabbit
BUILD SUCCESSFUL (total time: 21 seconds)
Here is a run that tests the exception thrown by the readFile method. I passed the readFile method a different file name as a parameter (not data.txt). Of course, that file name was not found which throws the exception FileNotFoundException which is catch in main:
run:
Please enter a word: testing
The word is: testing
FileNotFoundException: File not found
BUILD SUCCESSFUL (total time: 4 seconds)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started