Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a new Java application called \"CheckString\" (without the quotation mark

ID: 3918555 • Letter: C

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) 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.

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// CheckString.java

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Scanner;

public class CheckString {

                /**

                * method to check if a string is a valid word starting with a letter

                *

                * @param word

                *            - word to be checked

                * @throws Exception

                *             if the string is not a word

                */

                static void checkWord(String word) throws Exception {

                                if (word.length() > 0 && Character.isLetter(word.charAt(0))) {

                                                // do what you want to do if the word is valid

                                } else {

                                                throw new Exception("This is not a word.");

                                }

                }

                /**

                * method to prompt the user to enter a word, check it and return

                */

                static String getWord() {

                                Scanner scanner = new Scanner(System.in);

                                System.out.println("Enter the word: ");

                                // getting word

                                String word = scanner.next();

                                try {

                                                // checking

                                                checkWord(word);

                                                /**

                                                * if the checkWord method didn't throw any exception, the word is

                                                * valid

                                                */

                                                return word;

                                } catch (Exception e) {

                                                System.out.println(e.getMessage());

                                                // exception occured, asking again

                                                return getWord();

                                }

                }

                /**

                * method to write the contents of array to a file

                *

                * @throws FileNotFoundException

                *             -if the file cant be opened

                */

                static void writeFile(String[] arrayToWrite, String filename)

                                                throws FileNotFoundException {

                                PrintWriter writer = new PrintWriter(new File(filename));

                                /**

                                * looping through array element

                                */

                                for (int i = 0; i < arrayToWrite.length; i++) {

                                                // writing to file

                                                writer.println(arrayToWrite[i]);

                                }

                                // closing file

                                writer.close();

                }

                /**

                * method to read the contents of file into an array list

                *

                * @throws FileNotFoundException

                *             -if the file cant be opened

                */

                static ArrayList<String> readFile(String filename)

                                                throws FileNotFoundException {

                                ArrayList<String> fileContents = new ArrayList<String>();

                                Scanner scanner = new Scanner(new File(filename));

                                /**

                                * looping through each line

                                */

                                while (scanner.hasNext()) {

                                                // extracting line, and appending to array list

                                                String line = scanner.nextLine();

                                                fileContents.add(line);

                                }

                                // closing file

                                scanner.close();

                                return fileContents;

                }

                public static void main(String[] args) {

                                // getting and displaying word

                                String word = getWord();

                                System.out.println("Entered word: " + word);

                                /**

                                * defining an array of Strings, and testing writeFile and readFile

                                * methods

                                */

                                String testData[] = new String[] { "String1", "String2", "String3",

                                                                "String4" };

                                try {

                                                writeFile(testData, "data.txt");

                                                ArrayList<String> stringList = readFile("data.txt");

                                                System.out.println("File contents:");

                                                for (String s : stringList) {

                                                                System.out.println(s);

                                                }

                                } catch (FileNotFoundException e) {

                                                System.out.println(e.getMessage());

                                }

                }

}

/*OUTPUT*/

Enter the word:

1man

This is not a word.

Enter the word:

....yeah

This is not a word.

Enter the word:

{word}

This is not a word.

Enter the word:

Alright

Entered word: Alright

File contents:

String1

String2

String3

String4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote