Count - phrase = scan.nextLine...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
// ********************************************************** // // // This program reads in strings (phrases) and counts the // number of blank characters and certain other letters // in the phrase. // ********************************************************** import java.util.Scanner; public class Count { public static void main (String[] args) { String phrase; // a string of characters int countBlank; // the number of blanks (spaces) in the phrase int countA; // the number of As in the phrase int countE; // the number of Es in the phrase int countS; // the number of Ss in the phrase int countT; // the number of Ts in the phrase int length; // the length of the phrase char ch; // an individual character in the string Scanner scan = new Scanner (; // Print a program header System.out.println (); System.out.println ("Character Counter"); System.out.println (); // Read in a string and find its length System.out.print ("Enter a sentence or phrase, quit to end: ");
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: phrase = scan.nextLine(); while (!phrase.equals("quit")) { length = phrase.length(); // Initialize counts countBlank = 0; countA = 0; countE = 0; countS = 0; countT = 0; // a for loop to go through the string character by character // and count the blank spaces for (int i=0; i<length; i++) { ch = phrase.charAt(i); switch (ch) { case 'a': case 'A': countA++; break; case 'e': case 'E': countE++; break; case 's': case 'S': countS++; break; case 't': case 'T': countT++; break; case ' ': countBlank++; } } // Print the results System.out.println (); System.out.println ("Number of blank spaces: " + countBlank); System.out.println ("Number of As: " + countA); System.out.println ("Number of Es: " + countE); System.out.println ("Number of Ss: " + countS); System.out.println ("Number of Ts: " + countT); System.out.println (); //Get phrase for next time System.out.print ("Enter a sentence or phrase, quit to end: "); phrase = scan.nextLine(); } } }...
View Full Document

{[ snackBarMessage ]}

Page1 / 2

Count - phrase = scan.nextLine...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online