Write an application that counts the total number of spaces
Write an application that counts the total number of spaces contained in a movie quote entered by the user. Save the file as CountMovieSpaces2.java
Solution
CountMovieSpaces2.java
import java.util.Scanner;
public class CountMovieSpaces2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter the movie quote: \");
String quote = scan.nextLine();
int countSpaces = 0;
for(int i=0; i<quote.length(); i++){
if(quote.charAt(i) == \' \'){
countSpaces++;
}
}
System.out.println(\"The number of spaces contained in movie quote is: \"+countSpaces);
}
}
Output:
Enter the movie quote:
Greed, for lack of a better word, is good.
The number of spaces contained in movie quote is: 8
