Advertisement
Guest User

Character Counter

a guest
Nov 20th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. /**************************************************************/
  2. /* ----------This program was made by Orion Schyberg--------- */
  3. /*                                                            */
  4. /* This program takes an inputed word, and tells you what     */
  5. /* characters are what in the inputed string. When you enter  */
  6. /* the kill statement (9999), the system prints out the total */
  7. /* number of characters that were inputed. It classifies the  */
  8. /* characters as vowels, punctuation marks, consonants,       */
  9. /* spaces, or other characters (this means it takes anything  */
  10. /* character related)                                         */
  11. /**************************************************************/
  12.  
  13. package lab2;
  14. import java.util.*;
  15. //Importing the utility for the scanner
  16. public class Lab2 {
  17.     public static void main(String [] args) {
  18.         Scanner scan = new Scanner(System.in);
  19.         //Loads scanner
  20.         String word = "a";
  21.         //Base word
  22.         int v = 0, c = 0, p = 0, o = 0, s = 0;
  23.         //The counters for the characters
  24.         int vsum = 0, psum = 0, ssum = 0, csum = 0, osum = 0;
  25.         //Overall character counters
  26.         int counter = 0;
  27.         //This is a counter used for the int ch, and also as a regulator for the counter while loop
  28.         int stop = 0;
  29.         //Overall regulator
  30.        
  31.         while(stop != 1) {
  32.             //Unless stop = 1, this continues
  33.             System.out.println("Welcome to the Character Counter Game!");
  34.             System.out.print("To play, enter a word (Type '9999' to exit the program): ");
  35.             word = scan.nextLine();
  36.             //This changes the string in word to the inputed sentence/string/whatever
  37.             int ch = word.charAt(counter);
  38.             //The character at the counter
  39.             if(word.length() == 4 && word.contains("9999")) {
  40.                 stop++;
  41.                 break;
  42.                 //This is the killcode, which stops everything if '9999' gets inputed, but only if '9999' is put in
  43.                 //This means you could put in  '9 9 9 9' or '9191919'
  44.             }
  45.             while(counter != word.length()){
  46.                 //While the counter is less than the word length, this string loops
  47.                 word = word.toLowerCase();
  48.                 //This keeps things organized
  49.                 ch = word.charAt(counter);
  50.                 if(ch == 'a'||ch == 'e'||ch == 'i'||ch == 'o'||ch == 'u'){
  51.                     //Vowel checker - Checks for vowels at the current character, and if there are, it adds to the vowel counters
  52.                     v++;
  53.                     vsum++;                        
  54.                     counter++;
  55.                 }
  56.                 else if(ch == ' '){
  57.                     //Space checker - Checks for spaces at the current character, and if there are, it adds to the space counters
  58.                     s++;
  59.                     ssum++;
  60.                     counter++;
  61.                 }
  62.                 else if(ch == '!'||ch == '?'||ch == '.'||ch == ','||ch == ':'||ch == '-'||ch == ';'||ch == '('||ch == ')'||ch == '['||ch == ']'||ch == '{'||ch == '}'||ch == (char)39){
  63.                     //Punctuation checker - Checks for standard punctuation marks at the current character, and if there are, it adds to the punctuation counters
  64.                     p++;
  65.                     psum++;
  66.                     counter++;
  67.                 }
  68.                 else if(ch == 'b'||ch == 'c'||ch == 'd'||ch == 'f'||ch == 'g'||ch == 'h'||ch == 'j'||ch == 'k'||ch == 'l'||ch == 'm'||ch =='n'||ch == 'p'|| ch == 'q'||ch == 'r'||ch == 's'||ch == 't'||ch == 'v'||ch == 'w'||ch == 'x'||ch == 'y'||ch == 'z'){
  69.                     //Consonant checker - Checks for consonants at the current character, and if there are, it adds to the consonant counters
  70.                     c++;
  71.                     csum++;
  72.                     counter++;
  73.                 }
  74.                 else{
  75.                     //Throws everything else into the other counters
  76.                     o++;
  77.                     osum++;
  78.                     counter++;
  79.                     }
  80.                 }
  81.                     System.out.println("\nNumber of vowels: " +v);
  82.                     //Newline, prints out the number of vowels in the string
  83.                     System.out.println("Number of consonants: " +c);
  84.                     //Prints out the number of consonants in the string
  85.                     System.out.println("Number of spaces: " +s);
  86.                     //Prints out the number of spaces in the string
  87.                     System.out.println("Number of punctuation marks: " +p);
  88.                     //Prints out the number of other characters in the string
  89.                     System.out.println("number of other characters: " +o+ "\n");
  90.                     //Prints out the number of other characters in the string, creates a blank line
  91.                     v = 0;
  92.                     c = 0;
  93.                     s = 0;
  94.                     p = 0;
  95.                     o = 0;
  96.                     word = null;
  97.                     counter = 0;
  98.                     //These reset the ints and string back to the default, loops back
  99.             }
  100.                 //Now after the kill-code gets inputed
  101.                 System.out.println("\nTotal number of vowels: " +vsum);
  102.                 //New line, prints out the total number of vowels counted
  103.                 System.out.println("Total number of consonants: " +csum);
  104.                 //Prints out the total number of consonants counted
  105.                 System.out.println("Total number of spaces: " +ssum);
  106.                 //Prints out the total number of spaces counted
  107.                 System.out.println("Total number of punctuation marks: " +psum);
  108.                 //Prints out the total number of punctuation marks counted
  109.                 System.out.println("Total number of other characters: " +osum);
  110.                 //Prints out the total number of other characters counted
  111.                 System.out.println("Thanks for playing!");
  112.                 //Polite exit statement
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement