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 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 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 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.
Please can you draw a flow chart for the following code : Program code for Payroll,java:...
Please can you draw a flow chart for the following code : Program code for Payroll,java: public class Payroll { public Payroll(String name,int ID,double payRate) { this.name=name; this.ID=ID; this.payRate=payRate; } private String name; private double payRate,hrWorked; private int ID; public Payroll() { name="John Doe"; ID=9999; payRate=15.0; hrWorked=40; } public String getName() { return name; } public int getID() { return ID; } public void setPayRate(int payRate) { this.payRate=payRate; } public void setHrWorked(double hrWorked) { this.hrWorked=hrWorked; } public double getPayRate() {...
Problem 1: Recursive anagram finder please used Java please Write a program that will take a...
Problem 1: Recursive anagram finder please used Java please Write a program that will take a word and print out every combination of letters in that word. For example, "sam" would print out sam, sma, asm, ams, mas, msa (the ordering doesn't matter) input: cram output: cram crma carm camr cmra cmar rcam rcma racm ramc rmca rmac acrm acmr arcm armc amcr amrc mcra mcar mrca mrac macr marc Hints: Your recursive function should have no return value and...
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
[JAVA] You will write a program to validate passwords for users, making sure they meet the...
[JAVA] You will write a program to validate passwords for users, making sure they meet the following criteria: Rules: Passwords must be at least 8 characters long Passwords can only contain alpha numeric characters (no spaces or special characters) Passwords must contain at least 1 uppercase character Passwords must contain at least 1 lowercase character Passwords must contain at least 1 numeric character (0-9) Passwords cannot contain the word “password” A password that does not meet all of these rules...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT