Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************************************************/
- /* ----------This program was made by Orion Schyberg--------- */
- /* */
- /* This program takes an inputed word, and tells you what */
- /* characters are what in the inputed string. When you enter */
- /* the kill statement (9999), the system prints out the total */
- /* number of characters that were inputed. It classifies the */
- /* characters as vowels, punctuation marks, consonants, */
- /* spaces, or other characters (this means it takes anything */
- /* character related) */
- /**************************************************************/
- package lab2;
- import java.util.*;
- //Importing the utility for the scanner
- public class Lab2 {
- public static void main(String [] args) {
- Scanner scan = new Scanner(System.in);
- //Loads scanner
- String word = "a";
- //Base word
- int v = 0, c = 0, p = 0, o = 0, s = 0;
- //The counters for the characters
- int vsum = 0, psum = 0, ssum = 0, csum = 0, osum = 0;
- //Overall character counters
- int counter = 0;
- //This is a counter used for the int ch, and also as a regulator for the counter while loop
- int stop = 0;
- //Overall regulator
- while(stop != 1) {
- //Unless stop = 1, this continues
- System.out.println("Welcome to the Character Counter Game!");
- System.out.print("To play, enter a word (Type '9999' to exit the program): ");
- word = scan.nextLine();
- //This changes the string in word to the inputed sentence/string/whatever
- int ch = word.charAt(counter);
- //The character at the counter
- if(word.length() == 4 && word.contains("9999")) {
- stop++;
- break;
- //This is the killcode, which stops everything if '9999' gets inputed, but only if '9999' is put in
- //This means you could put in '9 9 9 9' or '9191919'
- }
- while(counter != word.length()){
- //While the counter is less than the word length, this string loops
- word = word.toLowerCase();
- //This keeps things organized
- ch = word.charAt(counter);
- if(ch == 'a'||ch == 'e'||ch == 'i'||ch == 'o'||ch == 'u'){
- //Vowel checker - Checks for vowels at the current character, and if there are, it adds to the vowel counters
- v++;
- vsum++;
- counter++;
- }
- else if(ch == ' '){
- //Space checker - Checks for spaces at the current character, and if there are, it adds to the space counters
- s++;
- ssum++;
- counter++;
- }
- else if(ch == '!'||ch == '?'||ch == '.'||ch == ','||ch == ':'||ch == '-'||ch == ';'||ch == '('||ch == ')'||ch == '['||ch == ']'||ch == '{'||ch == '}'||ch == (char)39){
- //Punctuation checker - Checks for standard punctuation marks at the current character, and if there are, it adds to the punctuation counters
- p++;
- psum++;
- counter++;
- }
- 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'){
- //Consonant checker - Checks for consonants at the current character, and if there are, it adds to the consonant counters
- c++;
- csum++;
- counter++;
- }
- else{
- //Throws everything else into the other counters
- o++;
- osum++;
- counter++;
- }
- }
- System.out.println("\nNumber of vowels: " +v);
- //Newline, prints out the number of vowels in the string
- System.out.println("Number of consonants: " +c);
- //Prints out the number of consonants in the string
- System.out.println("Number of spaces: " +s);
- //Prints out the number of spaces in the string
- System.out.println("Number of punctuation marks: " +p);
- //Prints out the number of other characters in the string
- System.out.println("number of other characters: " +o+ "\n");
- //Prints out the number of other characters in the string, creates a blank line
- v = 0;
- c = 0;
- s = 0;
- p = 0;
- o = 0;
- word = null;
- counter = 0;
- //These reset the ints and string back to the default, loops back
- }
- //Now after the kill-code gets inputed
- System.out.println("\nTotal number of vowels: " +vsum);
- //New line, prints out the total number of vowels counted
- System.out.println("Total number of consonants: " +csum);
- //Prints out the total number of consonants counted
- System.out.println("Total number of spaces: " +ssum);
- //Prints out the total number of spaces counted
- System.out.println("Total number of punctuation marks: " +psum);
- //Prints out the total number of punctuation marks counted
- System.out.println("Total number of other characters: " +osum);
- //Prints out the total number of other characters counted
- System.out.println("Thanks for playing!");
- //Polite exit statement
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement