In: Computer Science
A client at a software design company wants assistance processing their documents. After interviewing the client you find that their documents consist of a one sentence title, a date, and a series of paragraphs. Each paragraph is a simple list of sentences. Paragraphs are separated by new lines. Each document also has an author (first and last name) and a document access level (open, internal or confidential). At a minimal level the client would like the software to be able to: * Count the number of words in the document (with or without the title) * Count the number of characters in the document * Print out a document * Print out the date of the document * Print out the author of the document Additional desirable features: * Count the number of a particular type of word (noun, verb, proper noun or other) * Given a title, date, author and a string for the remaining document, create a document file * Spell check the document
JAVA Code :
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to
handle errors
import java.util.Scanner; // Import the Scanner class to read text
files
import java.io.FileWriter; // Import the FileWriter
class
import java.io.IOException; // Import the IOException class to
handle errors
public class Main {
public static void main(String[] args) {
int ch = 0;
Scanner sc= new
Scanner(System.in); //System.in is a standard
input stream
while(ch!=3){
System.out.println("Menu : ");
System.out.println("1. Read a File.");
System.out.println("2. Write to a File.");
System.out.println("3. Exit.");
ch = sc.nextInt();
switch (ch) {
case 1 :
try {
File doc1 = new File("Document1.txt"); //Create File Object to read
file "Document1.txt"
Scanner doc1Reader = new Scanner(doc1);
String title="", date="", author="", accessLevel="", data =
"";
String[] paragraph;
int numOfWordsWithTitle = 0, numOfWordsWithoutTitle = 0,
numOfCharactersWithTitle = 0, numOfCharactersWithoutTitle =
0;
title = doc1Reader.nextLine(); //Read Title from
Document1.txt
date = doc1Reader.nextLine(); //Read Date from Document1.txt
author = doc1Reader.nextLine(); //Read Author from
Document1.txt
accessLevel = doc1Reader.nextLine(); //Read Access Level from
Document1.txt
while (doc1Reader.hasNextLine()) {
data = data + doc1Reader.nextLine(); //Read Data from
Document1.txt
}
numOfCharactersWithTitle = title.length() + data.length();
//Calculate Number of Characters with title.
numOfCharactersWithoutTitle = data.length(); //Calculate Number of
Characters without title.
numOfWordsWithTitle = title.split(" ", 100).length;
numOfWordsWithoutTitle = 0;
paragraph = data.split("\n", 100);
for(int i = 0; i < paragraph.length; i++){
numOfWordsWithTitle = numOfWordsWithTitle + paragraph[i].split(" ",
1000).length; //Calculate Number of Words with title.
numOfWordsWithoutTitle = numOfWordsWithoutTitle +
paragraph[i].split(" ", 1000).length; //Calculate Number of Words
without title.
}
System.out.println("Document Data : ");
System.out.println(data); //Display Data.
System.out.println("Document Date : ");
System.out.println(date); //Display Date.
System.out.println("Document Author : ");
System.out.println(author); //Display Author.
System.out.println("Document Word count (with title) : ");
System.out.println(numOfWordsWithTitle); //Display Data Word count
(with title).
System.out.println("Document Word count (without title) : ");
System.out.println(numOfWordsWithoutTitle); //Display Data Word
count (without title).
System.out.println("Document Character count (with title) :
");
System.out.println(numOfCharactersWithTitle); //Display Data
Character count (with title).
System.out.println("Document Character count (without title) :
");
System.out.println(numOfCharactersWithoutTitle); //Display Data
Character count (without title).
System.out.println("Document Title : ");
System.out.println(title); //Display Title.
System.out.println("Document Access Level : ");
System.out.println(accessLevel); //Display Access Level.
doc1Reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
break;
case 2 :
try {
FileWriter doc2Writer = new FileWriter("Document2.txt");
String title, date, author, accessLevel, data = "";
sc.nextLine();
System.out.println("Enter Title : ");
title = sc.nextLine(); //Get Title from User.
System.out.println("Enter Date : ");
date = sc.nextLine(); //Get Date from User.
System.out.println("Enter Author Name (FirstName LastName) :
");
author = sc.nextLine(); //Get Author from User.
System.out.println("Enter Access Level (Open/Internal/Confidential)
: ");
accessLevel = sc.nextLine();
System.out.println("Enter Data : ");
data = sc.nextLine();
//Write to File.
doc2Writer.write(title + "\n");
doc2Writer.write(date + "\n");
doc2Writer.write(author + "\n");
doc2Writer.write(accessLevel + "\n");
doc2Writer.write(data + "");
doc2Writer.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
break;
}
}
}
}
Screenshots :
Code :
Document 1 :
Document 2 :
OutPut :