Question

I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according...

I've been assigned to 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

------------------------------------------------------CheckString.java-------------------------------------------------------

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Scanner;

public class CheckString {

public static void main(String[] args) {

//Calling the getWord method and print the result to the command line.

System.out.println("The word is : "+getWord());

String testData[]= {" Hii"," Hello"," Hey"};

//Calling the writeFile method to write in a file "data.txt".

writeFile(testData,"data.txt");

//ArrayList to store Strings of the given file

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

fileContents=readFile("data.txt"); //calling the readFile method

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

for(String content:fileContents) //printing ArrayList

{

System.out.println(content);

}

}

//checkWord method for checking first letter of String

public static void checkWord(String word) {

char first=word.charAt(0); // taking first letter of word string

try {

if(!Character.isLetter(first)) { //checking first letter for character or not

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

}

}catch(Exception e) {System.out.println(e);};

}

//getWord method for taking user input

public static String getWord() {

String word="";

//taking input from user

System.out.println("Enter a Word : ");

Scanner sc=new Scanner(System.in);

word=sc.next();

try {

checkWord(word); //calling the checkWord method

}catch(Exception e) {System.out.println(e);};

sc.close();

return word;

}

//writeFile method for write String array to a text file

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

try {

DataOutputStream dos=new DataOutputStream(new FileOutputStream(new File(filename)));

for(String word:arrayToWrite) {

dos.writeUTF(word+"\n"); //writing every word to file

}

}catch(IOException e) {System.out.println(e);}

}

public static ArrayList<String> readFile(String filename) {

ArrayList<String> listOfWords=new ArrayList<>(); //ArrayList of String

try {

Scanner sc=new Scanner(new File(filename)); //taking input from file

while(sc.hasNextLine()) { //checking if next line is their or not

listOfWords.add(sc.nextLine()); //Adding String to ArrayList

}

}catch(IOException e) {System.out.println(e);}

return listOfWords;

}

}

--------------------------------------------------Output-------------------------------------------------------------------

1)

Enter a Word :
jack
The word is : jack
File content :
Hii
Hello
Hey

2)

Enter a Word :
1jack
java.lang.Exception: This is not a word.
The word is : 1jack
File content :
Hii
Hello
Hey

Add a comment
Know the answer?
Add Answer to:
I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

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

  • create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in...

    create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below. The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain...

  • create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array...

    create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array of doubles (call it scores) with three rows and three columns and that uses methods and loops as follows. Use a method containing a nested while loop to get the nine (3 x 3) doubles from the user at the command line. Use a method containing a nested for loop to compute the average of the doubles in each row. Use a method to...

  • create a new Java application called "RecursiveTriangle" (without the quotation marks) according to the following guidelines....

    create a new Java application called "RecursiveTriangle" (without the quotation marks) according to the following guidelines. Modify the example in Horstmann Section 5.9, pp. 228-230 so that the triangle displays “in reverse order” as in the example below, which allows the user to set the number of lines to print and the String used for printing the triangle. Use a method to prompt the user for the number of lines (between 1 and 10) to print. This method should take...

  • create a new Java application called "WeightedAvgWithExceptions" (without the quotation marks), according to the following guidelines...

    create a new Java application called "WeightedAvgWithExceptions" (without the quotation marks), according to the following guidelines and using try-catch-finally blocks in your methods that read from a file and write to a file, as in the examples in the lesson notes for reading and writing text files. Input File The input file - which you need to create and prompt the user for the name of - should be called 'data.txt', and it should be created according to the instructions...

  • create a new Java application called "MinMax" (without the quotation marks) that declares an array of...

    create a new Java application called "MinMax" (without the quotation marks) that declares an array of doubles of length 5, and uses methods to populate the array with user input from the command line and to print out the max (highest) and min (lowest) values in the array.

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes...

    Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes three integer values into the first three positions of an array of integers calls a method you write called doubleEachValue that takes an array of integers (the one whose values you hard-coded in main) as its only argument and returns an ArrayList of integers, with each value in returned ArrayList equal to double the correspondingly indexed value in the array that is passed in...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT