Respuesta :
Answer:
The Java code is given below
Explanation:
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class StringCheck{
//There are several implementation approaches. This is one example framework/outline which might be helpful. Please feel free to try 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
if(Character.isLetter(word.charAt(0))){
return;
}else{
throw new Exception("This is not a word");
}
}
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
Scanner sc = new Scanner(System.in);
boolean st = true;
String word = null;
while(st){
System.out.println("Enter a Word : ");
word = sc.next();
try{
checkWord(word);
st = false;
}catch(Exception e){
System.out.println(e);
st = true;
}
}
sc.close();
return word;
}
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
for(int i = 0;i<arrayToWrite.length;i++){
fileWordStream.write(arrayToWrite[i]+System.lineSeparator());
}
fileWordStream.flush();
fileWordStream.close();
// In the loop use the lineSeparator method to put each string on its own line
}
public static ArrayList readFile(String filename) throws FileNotFoundException, IOException {
// Declare local ArrayList
ArrayList<String> words = new ArrayList<>();
// Create a new File object using filename parameter
File file = new File(filename);
// Check if the file exists, if not throw a new exception
if(!file.exists()){
throw new FileNotFoundException("File not Found");
}
// Create a new BufferedReader object
Scanner fsc = new Scanner(file);
// use a loop and the readLine method to read each string (on its own line) from the file
while(fsc.hasNextLine()){
words.add(fsc.nextLine());
}
fsc.close();
// return the filled ArrayList to main
return words;
}
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
ArrayList<String> words = new ArrayList<>();
// Declare a string variable containing the file name "data.txt"
String file = "data.txt";
// Call getWord, assign the returned string to a variable and display it
String word = getWord();
System.out.println("The word is : "+word);
// Call writeFile and readFile methods in a try block
try{
writeFile(testData,file);
words = readFile(file);
// Printout the contents of the ArrayList after the call to readFile
for(int i = 0;i<words.size();i++){
System.out.println(words.get(i));
}
}catch(FileNotFoundException e){
System.out.println(e);
}catch(IOException e){
System.out.println("IOException: error occured in input output");
}
// catch two types of exceptions
}
}