Advertisement
Guest User

Counter

a guest
Nov 20th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. package counter;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Counter
  7. {
  8. public static void main (String[] args)
  9. {
  10. String phrase = null; // a string of characters
  11. int countBlank; // the number of blanks (spaces) in the phrase
  12. int length, countA=0, countT=0, countS=0, countE=0; // the length of the phrase
  13. char ch; // an individual character in the string
  14. int i=0;
  15. Scanner scan = new Scanner(System.in);
  16.  
  17. // Print a program header
  18. System.out.println ();
  19. System.out.println ("Character Counter");
  20. System.out.println ();
  21.  
  22. // Read in a string and find its length
  23. System.out.print ("Enter a sentence or phrase (quit to stop): ");
  24.  
  25.  
  26. // Initialize counts
  27. countBlank = 0;
  28. // a for loop to go through the string character by character and count
  29. ch = ' ';
  30. // the blank spaces. Print the results
  31. String quit = null;
  32.  
  33. while (!phrase.equals("quit"))
  34. {
  35. phrase = scan.nextLine();
  36. phrase = phrase.toLowerCase();
  37. length = phrase.length();
  38. for (i = 0; i < phrase.length(); i++)
  39. {
  40. ch = phrase.charAt(i);
  41.  
  42. switch (ch)
  43. {
  44. case 'a':
  45. countA++;
  46. break;
  47. case 'e':
  48. countE++;
  49. break;
  50. case 's':
  51. countS++;
  52. break;
  53. case 't':
  54. countT++;
  55. break;
  56. case ' ': countBlank++;
  57. break;
  58. }
  59. }
  60.  
  61. break;
  62. }
  63.  
  64.  
  65. System.out.println ("Number of blank spaces: " + countBlank);
  66. System.out.println ("Number of A's: " +countA);
  67. System.out.println("Number of E's: " +countE);
  68. System.out.println ("Number of T's: " + countT);
  69. System.out.println ("Number of S's: " + countS);
  70.  
  71. }
  72.  
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement