Question & Answer: create a new Java application called "CheckString" (without the quotation marks) according to the following…..

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: “Exception found – invalid word due to no starting letter.”

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 testArrayData and populate it with at least four 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.

Expert Answer

 

package checkstring;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Scanner;

class InvalidWordException extends Exception{

InvalidWordException(String s){

super(s);

}

 

}

public class CheckString {

/**

* @param args the command line arguments

*/

 

public static void main(String[] args) {

// TODO code application logic here

 

CheckString obj=new CheckString();//object of checkString class

 

obj.getWord(); //calling of getWord method

 

String[] testArrayData = new String[4];

testArrayData[0]=”this is first line of string.n”;

testArrayData[1]=”this is second line of string.n”;

testArrayData[2]=”this is Third line of string.n”;

testArrayData[3]=”this is Forth line of string.n”;

 

try{

obj.writeFile(testArrayData, “data.txt”);

}

catch(IOException ex){

System.out.print(ex);

}

 

try{

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

fileContents=obj.readFile(“data.txt”);

// loop to print the contents of the fileContents ArrayList to the command line.

int arrayListSize = fileContents.size();

for(int i = 0; i < arrayListSize; i++)

{

System.out.println(fileContents.get(i));

}

}

catch(IOException ex){

System.out.print(ex);

}

 

 

}

 

//getword method

public String getWord(){

Scanner scan=new Scanner(System.in);

System.out.print(“please enter a word”);

String str = scan.next();

 

try{

//calling of checkWord method

checkWord(str);

}catch(Exception m){System.out.println(“Exception occured: “+m);}

 

return str;

}

 

//checkWord method

public void checkWord(String word) throws InvalidWordException{

if (!Character.isLetter(word.charAt(0))){

throw new InvalidWordException(“Exception found – invalid word due to no starting letter.”);

}

}

 

//writeFile method

public void writeFile(String arrayToWrite[],String FileName) throws IOException{

File file = new File(FileName);

 

// creates the file

file.createNewFile();

 

// creates a FileWriter Object

FileWriter writer = new FileWriter(file);

 

// Writes the content to the file

for(int inc = 0; inc <= arrayToWrite.length-1; inc++){

writer.write(arrayToWrite[inc],0,arrayToWrite[inc].length());

}

 

writer.flush();

writer.close();

}

 

//readFile method

public ArrayList readFile(String fileName) throws IOException{

Scanner s = new Scanner(new File(fileName));

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

while (s.hasNextLine()){

list.add(s.nextLine());

}

s.close();

return list;

}

 

 

}

output:

run:

please enter a wordrt

this is first line of string.

this is second line of string.

this is Third line of string.

this is Forth line of string.

BUILD SUCCESSFUL (total time: 12 seconds)

Still stressed from student homework?
Get quality assistance from academic writers!