Question

In: Computer Science

How many words are in the Gettysburg Address? Write a program that reads any text file,...

How many words are in the Gettysburg Address?

Write a program that reads any text file, counts the number of characters, num- ber of letters and number of words in the file and displays the three counts.

To test your program, a text file containing Lincoln’s Gettysburg Address is included on the class moodle page.

Sample Run

Word, Letter, Character Count Program

Enter file name: GettysburgAddress.txt

Word Count      =  268
Letter Count    = 1149
Character Count = 1440

Do the following

  1. To detect is a character ch is a letter use the following if statement if ch >= ‘A’ and ch <= ‘Z’ or ch >= ‘a’ and ch <= ‘z’:

    You may want to embed this “if statement” in a Boolean valued function (isLetter(ch)) that returns True if ch is a letter; otherwise it returns False

  2. Ask the user for the file name.

  3. For this assignment, the easiest way do the following three counts is to first input the text file as one very long string using the read() method. Don’t use readline() or readlines()here, unless using the read() method crashes the program.

  4. The character count is simply the length of the string you input from Step 3 above. Use the len() function

1

  1. To determine the letter count you will need to go through the string character by character using a for loop testing if each character is a letter or not. If it is a letter, increment a letter count variable.

  2. Checking for the number of words is easily done using the split method for strings (see page 136 for details). Recall that string.split( ) was used to break up a date such as “10/25/2016” into a list of three strings. That is "10/25/20165".split("/") returns the list ["10", "25", "2016"].

    By default if no “split” character is given, the string will be split wherever a space occurs. This makes it perfect to split a text string into a list of individual words which then can be counted by using the len() to get the length of the list.

  3. Output your three counts using string formatting to properly line up your three counts and close the file

Solutions

Expert Solution

Short Summary:

Implemented the program as per requirement
Attached source code and sample output

(Updated the code with if block as mentioned in the question)


**************Please do upvote to appreciate our time. Thank you!******************

Source Code:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Scanner;

/** This class reads input sentence from a file and prints the no of words, characters and letters**/
public class StringOpsMain {

   public static void main(String[] args) {

       try {
           BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\asus\\Input.txt"));

           calculateAndPrintChars(bufferedReader.readLine());


           bufferedReader.close();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }

   }
  

//This method prints the no of characters,letters and words in the given input string
   static void calculateAndPrintChars(String sentence)
   {
       //Form a string array
       String[] sentenceArray=sentence.split(" ");
       System.out.println("Word Count = "+sentenceArray.length);
       int letterCount=0;
       //Loop to find if its a letter
       for(int i=0;i<sentence.length();i++)
       {
           if(isLetter(sentence.charAt(i)))
               letterCount++;
       }
       System.out.println("Letter Count = "+sentence.length());
       System.out.println("Character Count = "+letterCount);

   }

   static boolean isLetter(char ch)
   {
       if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
           return true;
       return false;
   }

}

Code Screenshot:

Output:


**************Please do upvote to appreciate our time. Thank you!******************


Related Solutions

Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT