Question

In: Computer Science

Java please! Write the code in Exercise.java to meet the following problem statement: Write a program...

Java please!

Write the code in Exercise.java to meet the following problem statement:

Write a program that will print the number of words, characters, and letters read as input. It is guaranteed that each input will consist of at least one line, and the program should stop reading input when either the end of the file is reached or a blank line of input is provided.

Words are assumed to be any non-empty blocks of text separated by spaces, and words will not cross over a line boundary. It is also assumed that there will be no whitespace characters before or after each line of input.

To determine if a character is a letter, use the Character.isLetter() static method.

EX:

input:

This is a sentence. There are 10 words in it.

output:

Words: 10

Characters: 44

Letters: 32

Solutions

Expert Solution

Short Summary:

  • Implemented the program as per the requirement

Source Code:

package com.core.java;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

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

   public static void main(String[] args) {

       try {
           //Get file input using scanner
           Scanner input=new Scanner(new File("C:\\Users\\asus\\Input.txt"));
           while(input.hasNext())
           {
               String sentence=input.nextLine();
               //Stop reading input if a blank line is found
               if(sentence!=null&&sentence.length()>0)
               calculateAndPrintChars(sentence);
               else
                   break;
           }
           input.close();
       } catch (FileNotFoundException e) {
           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("Input:\n"+sentence);
       System.out.println("Output\nWords:"+sentenceArray.length);
       int letterCount=0;
       //Loop to find if its a letter
       for(int i=0;i<sentence.length();i++)
       {
           if(Character.isLetter(sentence.charAt(i)))
               letterCount++;
       }
       System.out.println("Characters:"+sentence.length());
       System.out.println("Letters:"+letterCount);

   }

}

Code Screenshot:

Output:


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


Related Solutions

Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Java Complete Example.java to fulfill the following problem statement: Write a program to accept a list...
Java Complete Example.java to fulfill the following problem statement: Write a program to accept a list of numbers, one per line. Input is provided from a file if one is provided as the first command line argument. If a file is not provided, input should be read from the terminal. The numbers can either be whole numbers or floating point numbers. The program should continue to accept input until a number equal to 00 is input. Of course, if the...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.                                                                   Requirements The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
Please write a java code. Write a generic program for New Home Construction Pricing with the...
Please write a java code. Write a generic program for New Home Construction Pricing with the following specifications. Note, each one of the 2, 3 or 4 bedroom homes are priced with standard type bathrooms. Update to deluxe or premium choice of bathrooms can be ordered by paying the difference in prices.    Types of homes Price 2 bedroom, 2 bathroom (standard type) and 1 car garage home = $350,000 3 bedroom, 2 bathroom (standard type) and 2 car garage...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods to   1) read the content of an array of 5 doubles public static double[] readingArray() 2) find and print:the smallest element in an array of 5 double public static void smallest(double [] array) 3) find and print:the largest element in an array of 5 doubles pubic static void largest (double [] array) In the main method - invoke readingArray and enter the following numbers...
Please code by Java. The following preemptive program with this problem after sample output(fill random number,...
Please code by Java. The following preemptive program with this problem after sample output(fill random number, recursive search, non-recursive search, exit), You just need to modify this code to add sorting and time calculations. Problem Description: Expand on Lab3 by adding methods that implement Bubble Sort, Selection Sort, Insertion Sort, Merge Sort and Quick Sort for displaying the sorted integers from the randomly generated array. NOTE: Create separate methods for each type of sort. Run Tests for all 4 types...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based on a problem in the C++ text by Friedman & Koffman: The results of a survey of the households in your township are available for public scrutiny. Each record (struct-type entity) contains input data for one household, including a four-digit integer identification number the annual income for the household the number of household members. Assuming that no more than 25 households were surveyed, write...
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A...
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A palindromic number is an integer that is the same when the digits are reversed. For example, 121 and 625526 are palindromic, but 625 is not a palindromic number. Input: The input is in ‘palindrome.txt’. The first line of the input contains the line count m (1 ≤ m ≤ 1,000), which is the number of lines that follows the first line. Each of the...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT