Question

In: Computer Science

Write a Java program to read in text line by line until a blank line is...

Write a Java program to read in text line by line until a blank line is entered. When this occurs the program returns the total number of words and characters found as shown in the example below.

A program to count the number of words and characters in an input text block.

Enter text line by line; blank line to end.

>> This is the first line of text

>> Followed by the second line of text

>> Ending with the final line of text

>>

Your text contained 21 words and 81 characters.

Your program should be able to reproduce this example (Note that text following the >> prompt is user input to the program).

Instructions

1. Start by writing a method, countLine, to count the number of words and characters in a string. It should have the following signature:

private countObject countLine(String input) {

which returns the total number of words and characters in the input string, where countObject is a simple nested class similar to listNode with two instance variables corresponding to the number of words and characters respectively.

class countObject {

            int words;

            int chars;

}

Hint: you might consider adding a constructor to initialize this object and getter methods to retrieve each component.

2. Write a main class which implements the user dialog shown in the example. It calls the countLine method to process each line read and adds the counts returned in countObject to the totals. Call this class wordCount.

Hints: You may use that acm.jar is available; the StringTokenizer class can greatly simplify the coding of the countLine method.  

Do your coding in Eclipse. Cut and paste wordCount.java into the space provided on the exam.

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

import java.util.Scanner;

public class Main {

    class countObject {
        int words;
        int chars;

        public countObject(String line) {
            words = line.split("\\s+").length;
            chars = line.length() - words + 1;
        }

        public int getWords() {
            return words;
        }

        public int getChars() {
            return chars;
        }
    }

    private countObject countLine(String input) {

        countObject co = new countObject(input);
        return co;
    }

    public static void main(String[] args) {

        Main mc = new Main();
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter text line by line; blank line to end.\n" + "\n");

        int words = 0, characters = 0;
        String input;
        do {
            input = scanner.nextLine();
            countObject co = mc.countLine(input);

            words += co.getWords();
            characters += co.getChars();

        } while (input.length() != 0);

        System.out.println("Your text contained " + (words-1) + " words and " + characters + " characters.");


    }
}

==============================================================


Related Solutions

in java (just a line of code will do) Your program will read in the text...
in java (just a line of code will do) Your program will read in the text file, word by word. You are to ignore leading and trailing punctuation and capitalization (i.e., "Castle", "castle?", and "CASTLE" are all equivalent to castle). Convert all words to lower case on input, or you can use case-insensitive comparisons - your choice. You will be putting the words into several implementations of linked lists and comparing the results. If, after trimming punctuation, a "word" consists...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
Write a program that will read a line of text. Display all the letters that occure...
Write a program that will read a line of text. Display all the letters that occure in the text, one per line and in alphabetical order, along with the number of times each letter occurs in the text. Use an array of base type int of length 26, so that the element at index 0 contains the number of a’s, the element at index 1 contains the number of b’s, and so forth, Alloow both upperCase and lower Case. Define...
Write a program that will read in from input file one line at a time until...
Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces, commas and periods....
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
Write a complete C program that read the text below and save the text in a...
Write a complete C program that read the text below and save the text in a new file "second.txt" with the same text written in all uppercase. "Beedle the Bard was an English wizard and author of wizarding fairytales. Beedle was born in Yorkshire, England. At some point in his life he wrote The Tales of Beedle the Bard . The only known image of Beedle is a woodcut that shows him with a "luxuriant" beard. Beedle wrote in ancient...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT