Jamiemcg

Yahoo! Answers - Counting Letters in a Word (Java)

Oct 25th, 2014
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. /* Yahoo! Answers Question Response
  2.  * Question URL: http://yhoo.it/1nDAhfI
  3.  * Author: Jamie
  4.  * TechnicalCafe.com
  5.  */
  6.  
  7. //import Scanner class to get user input
  8. import java.util.Scanner;
  9.  
  10. public class LetterCount{
  11.  
  12.     public static void main(String [] args){
  13.        
  14.         //define and declare variables to hold the number of times
  15.         //the letters c, d, h, m, and t have been found in a word
  16.         int c = 0;
  17.         int d = 0;
  18.         int h = 0;
  19.         int m = 0;
  20.         int t = 0;
  21.        
  22.         //instantiate an object of the Scanner class so that
  23.         //we can get user input
  24.         Scanner input = new Scanner(System.in);
  25.        
  26.         //prompt the user to enter a word
  27.         System.out.println("Please enter a word!");
  28.        
  29.         //define and declare a String variable to hold the value of
  30.         //the word that is entered by the user and then use the Scanner
  31.         //class object, called "input" to scan for a word (String)
  32.         String word = input.nextLine();
  33.        
  34.         //use the ".length()" method of the String class to find out
  35.         //how long the user-entered String is and store that value in
  36.         //value of type "int"
  37.         int wordLength = word.length();
  38.        
  39.         //Now that we know what the length of the String is, we can
  40.         //use a for loop to iterate through each letter of the word
  41.         //and use conditional statements to count how many times the
  42.         //letters c, d, h, m, and t appear within that String/word
  43.         for(int i = 0; i < wordLength; i++){
  44.            
  45.             if(word.substring(i, i+1).equals("c") || word.substring(i, i+1).equals("C")){
  46.                 //increment the variable for the letter "c" by one
  47.                 c++;
  48.             }
  49.             else if(word.substring(i, i+1).equals("d") || word.substring(i, i+1).equals("D")){
  50.                 //increment the variable for the letter "d" by one
  51.                 d++;
  52.             }
  53.             else if(word.substring(i, i+1).equals("h") || word.substring(i, i+1).equals("H")){
  54.                 //increment the variable for the letter "h" by one
  55.                 h++;
  56.             }
  57.             else if(word.substring(i, i+1).equals("m") || word.substring(i, i+1).equals("M")){
  58.                 //increment the variable for the letter "m" by one
  59.                 m++;
  60.             }
  61.             else if(word.substring(i, i+1).equals("t") || word.substring(i, i+1).equals("T")){
  62.                 //increment the variable for the letter "t" by one
  63.                 t++;
  64.             }
  65.             else{
  66.                 //if the word does not contain any of the five letters
  67.                 //the program is counting, then we can just ignore that
  68.                 //letter and move on to the next letter within the word
  69.                
  70.             }
  71.            
  72.         }
  73.        
  74.         //Now that the loop has finished iterating, we can use some
  75.         //System.out.println() statements to print the results of the
  76.         //count to the user
  77.         System.out.println("Results of letter count in the word:"
  78.                             + " " + word + ":");
  79.         System.out.println("c:" + c);
  80.         System.out.println("d:" + d);
  81.         System.out.println("h:" + h);
  82.         System.out.println("m:" + m);
  83.         System.out.println("t:" + t);
  84.  
  85.     }
  86.  
  87. }
Add Comment
Please, Sign In to add comment