import java.util.*;
public class TotalWords{
Scanner scan=new Scanner(System.in);
String text;
public void input(){
try{
System.out.printf("Enter A String: ");
text=scan.nextLine();
} catch(InputMismatchException e){
System.err.println("Error Occurred!\n"+e.getMessage());
System.exit(0);
}
}
public void compute(){
scan=new Scanner(text);
long total=0;
while(scan.hasNext()){
scan.next();
total++;
}
System.out.println("Total Number of Words: "+total);
}
public static void main(String[] args){
TotalWords TW=new TotalWords();
TW.input();
TW.compute();
}
}
Comments