Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

wrte code for the main method which uses a loop to collect individual words the

ID: 3597330 • Letter: W

Question

wrte code for the main method which uses a loop to collect individual words the user by calling the readString method provided.

The number of the words collected should be counted.Each new word collected should be appended to a string called sentences.

when a fullstop(".") is typed by the user end the loop and print the sentences including the fullstop.print out how many words are in the sentences.Make sure the words count does not include the fullstop.Include any declaration and initialisations.

write code in java

Explanation / Answer

import java.util.Scanner;
class count
{
void readString() //function to read string
{
String str,sentence=""; //variable declaration
int c=-1;
char ch;
Scanner sc = new Scanner(System.in);
do
{
System.out.println("Enter a word "); //accepting word from user
str=sc.next();
ch=str.charAt(0);
sentence+=str; //joinging the word
c++; //counting the number of word
}while(ch!='.');
System.out.println(sentence);
System.out.println("The number of words is "+c); //printing the result
}
public static void main(String args[])
{
count obj=new count(); //creating object of the class
obj.readString(); //calling the function
}
}