Counting Characters Countjava The file Countjava contains t

Counting Characters - Count.java The file Count.java contains the skeleton of a program to read in a string (a sentence or phrase) and count the number of blank spaces in the string. The program currently has the declarations and initializations and prints the results. All it needs is a loop to go through the string character by character and count (update the countBlank variable) the characters that are the blank space. Since we know how many characters there are (the length of the string) we use a count controlled loop - for loops are especially well-suited for this. Add the for loop to the program. Inside the for loop you need to access each individual character the charAt method of the String class lets you do that. The assignment statement ch = phrase.charAt(i); assigns the variable ch (type char) the character that is in index i of the String phrase. In your for loop you can use an assignment similar to this (replace i with your loop control variable if you use something other than i). Test your program on several phrases to make sure it is correct. Now modify the program so that it will count several different characters, not just blank spaces. To keep things relatively simple we’ll count the a’s, e’s, s’s, and t’s (both upper and lower case) in the string. You need to declare and initialize four additional counting variables (e.g. countA and so on). Your current if could be modified to cascade but another solution is to use a switch statement. Replace the current if with a switch that accounts for the 9 cases we want to count (upper and lower case a, e, s, t, and blank spaces). NOTE: You could also directly use phrase.charAt(i) in your if (without assigning it to a variable). The cases will be based on the value of the ch variable. The switch starts as follows: switch (ch) { case \'a\': case \'A\': countA++; break; case .... } Note that this switch uses the “fall through” feature of switch statements. If ch is an ‘a’ the first case matches and the switch continues execution until it encounters the break hence the countA variable would be incremented. Add statements to print out all of the counts. Be sure to go through the program and properly indent after adding code with nested loops the inner loop should be indented. // ************************************************************ // Count.java // // 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 length; // the length of the phrase char ch; // an individual character in the string Scanner scan = new Scanner(System.in); // 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: \"); phrase = scan.nextLine(); length = phrase.length(); // Initialize counts countBlank = 0; // a for loop to go through the string character by character // and count the blank spaces // Print the results System.out.println(); System.out.println(\"Number of blank spaces: \" + countBlank); System.out.println(); } } Sample run: Character Counter Enter a sentence or phrase: programming means to create (or develop) software, which is also called a program. Number of blank spaces: 12 Number of As: 8 Number of Es: 7 Number of Ss: 4 Number of Ts: 3

Solution

Count.java

package com.examples;

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 length; // the length of the phrase
char ch; // an individual character in the string
Scanner scan = new Scanner(System.in);
// 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: \");
phrase = scan.nextLine();
length = phrase.length();
// Initialize counts
countBlank = 0;
int countA = 0;
int countE = 0;
int countS = 0;
int 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++;
   break;
}
}
// Print the results
System.out.println ();
System.out.println (\"Number of A Letters: \" + countA);
System.out.println (\"Number of E Letters: \" + countE);
System.out.println (\"Number of S Letters: \" + countS);
System.out.println (\"Number of T Letters: \" + countT);
System.out.println (\"Number of blank spaces: \" + countBlank);
System.out.println ();
}
}

Output:


Character Counter

Enter a sentence or phrase: programming means to create (or develop) software, which is also called a program.

Number of A Letters: 8
Number of E Letters: 7
Number of S Letters: 4
Number of T Letters: 3
Number of blank spaces: 12

Counting Characters - Count.java The file Count.java contains the skeleton of a program to read in a string (a sentence or phrase) and count the number of blank
Counting Characters - Count.java The file Count.java contains the skeleton of a program to read in a string (a sentence or phrase) and count the number of blank

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site