Question

In: Computer Science

In java P4.6 Following Section 4.9 develop a program that reads text and displays the average...

In java P4.6 Following Section 4.9 develop a program that reads text and displays the average number of words in each sentence. Assume words are separated by spaces, and a sentence ends when a word ends in a period. Start small and just print the first word. Then print the first two words. Then print all words in the first sentence. Then print the number of words in the first sentence. Then print the number of words in the first two sentences. Then print the average number of words in the first two sentences. At this time, you should have gathered enough experience that you can complete the program.

Solutions

Expert Solution

Here is the code for the above problem with output snippet. I have directly calculated the average number of words in a sentence. If you need further assistance please let me know.

// import Scanner class for input
import java.util.Scanner;
public class HelloWorld{

     public static void main(String []args){

        // Scanner class object for taking input with spaces 
        Scanner in = new Scanner(System.in);

        // takes the input from the user
        String str = in.nextLine();
        System.out.println(str);
        
        // have space counter and period counter initialized to zero
        int space_count = 0;
        int per_count = 0;

        // for loop till the end of the input string
        for(int i=0; i<str.length(); i++)
        {
            // count number of spaces 
            if(str.charAt(i) == ' ')
            space_count++;
            
            // count the number of periods
            if(str.charAt(i) == '.')
            per_count++;
        }
        
        // print the average number of words = total words/ total sentences
        System.out.printf("Average number of words per sentence are : %d", (space_count+1)/per_count);
     }
}

Here is the output snippet of the code


Related Solutions

In java P4.7 Following Section 4.9, develop a program that reads a string and removes all...
In java P4.7 Following Section 4.9, develop a program that reads a string and removes all duplicates. For example, if the input is Mississippi, print Misp. Start small and just print the first letter. Then print the first letter and true if the letter is not duplicated elsewhere, false otherwise. (Look for it in the remaining string, by using the substring and indexOf method). Next, do the same for the first two letters, and print out for each letter whether...
P4.7 Following Section 4.9, develop a program that reads a string and removes all duplicates. For...
P4.7 Following Section 4.9, develop a program that reads a string and removes all duplicates. For example, if the input is Mississippi, print Misp. Start small and just print the first letter. Then print the first letter and true if the letter is not duplicated elsewhere, false otherwise. (Look for it in the remaining string, by using the substring and indexOf method). Next, do the same for the first two letters, and print out for each letter whether or not...
Write a Java program that reads words from a text file and displays all the non-duplicate...
Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra
Write a Java program that reads a name and displays on the screen.
Write a Java program that reads a name and displays on the screen.
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
Write a Java program that reads two integers on the keyboard and displays them on the...
Write a Java program that reads two integers on the keyboard and displays them on the screen.
Write a Java program that reads a whole number on the keyboard and obtains and displays...
Write a Java program that reads a whole number on the keyboard and obtains and displays twice and triple that number on the screen
● 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 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. 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 IS PREFERRED THANK YOU IN ADVANCE...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT