Learning Java in 2020 – Beginner – Count Vowels

Overview reference

Problem

Goal

Count the number of vowels in a given text and provide a report of how many times a specific vowel occurred in the text.

Pseudo-code

Count Vowels

  • Get user input
  • Declare a count variable and initialize with 0
  • Loop through text
  • Check if each character is a vowel
  • If vowel, increase count value
  • Print out count

Generate Vowel Occurrence Report

  • Get user input
  • Create a count variable for each vowel and set them each to 0
  • Loop through text
  • Check if each character is a vowel
  • If vowel, increase corresponding vowel count variable by 1
  • Print out report

Solution

Check is vowel

private static boolean characterisvowel(char character) {
		String vowels = "aeiou";
		boolean isvowel = false;
		if(vowels.indexOf(character) >= 0) {
			isvowel = true;
		}
		
		return isvowel;
	}

 

Unlike the Pig latin exercise,an alternative approach is taken in this characterisvowel method. Instead of looping through the vowels variable “aeiou” and comparing the argument against each character ,the occurrence of the argument is determined in the vowels variable using the String.indexOf method. With an index >= 0, true is returned, else false is returned.

Count vowels in text

private static int countVowelsPerWord(String word) {
		int count = 0;
		for(int i = 0; i < word.length(); i++) {
			char current_char = word.charAt(i);
			if(CountVowels.characterisvowel(current_char)) {
				count += 1;
			}
		}
		return count;
	}

 

As already stated in the pseudo-code section,the count variable is incremented by 1 each time a character in the given String is a vowel.

Generate vowel occurrence report

Coming from javascript, I thought of using the datatype object or dictionary in python. So I searched “object or dictionary in java” with hopes of finding a data type, with which data could be stored in a key -> value format.

Such actually exist but it is called HashMap. According to this article, it is a Map based collection used to store key and value pairs. Exactly what was needed.

Steps

  • Loop through each character in the given string
  • Check if character already exists as a key in the hashmap instance
  • If it does, simply increment its value by 1
  • Else, I check if it is a vowel
    • If it is a vowel, a new key -> value pair is added
    • the key is set to the current character and the value to 0.
  • Loop through key-> value pair of the hashmap instance
  • A report text is generated usings the key and value pair
  • Return report text
private static String generatevowelreport(String text) {
		String report_text = "Here is your vowel report: \n";
		HashMap<Character, Integer> report = new HashMap<Character, Integer>();
		
		for(int i = 0; i < text.length(); i++) {
			char current_char = text.charAt(i);
			if(report.containsKey(current_char)) {
				int newnumber = report.get(current_char);
				newnumber += 1;
				report.put(current_char, newnumber);
			}
			else if(CountVowels.characterisvowel(current_char)) {
				report.put(current_char, 1);
			}
		}
		
		
		for(Map.Entry<Character, Integer> eachvowel : report.entrySet()) {
			 char vowel = eachvowel.getKey();
			 int value = eachvowel.getValue();
			 report_text += vowel +  " occurs " + value + " times \n";
		}
		return report_text;
	}

 

Trigger Action

Anyone following my learning programming with -> java should immediately recognize this snippet. This just obtains input from the user till he/she presses Enter. Then counts the number of vowels, generates a vowel occurrence report and prints out the result.

public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Please type in a text");
		Scanner sc = new Scanner(System.in);
		String text = sc.nextLine();
		int numberofvowels = CountVowels.countVowelsPerWord(text);
		String vowelreport = CountVowels.generatevowelreport(text);
		
		System.out.println("The number of vowels in your text above is : " + numberofvowels);
		System.out.println(vowelreport);
	}

Run Program

C:<path>\com.learnjava\src\com\learnjava\beginner>java CountVowels.java
Please type in a text
person in a life of crisis
The number of vowels in your text above is : 9
Here is your vowel report:
a occurs 1 times
e occurs 2 times
i occurs 4 times
o occurs 2 times

CountVowels.java

package com.learnjava.beginner;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class CountVowels {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Please type in a text");
		Scanner sc = new Scanner(System.in);
		String text = sc.nextLine();
		int numberofvowels = CountVowels.countVowelsPerWord(text);
		String vowelreport = CountVowels.generatevowelreport(text);
		
		System.out.println("The number of vowels in your text above is : " + numberofvowels);
		System.out.println(vowelreport);
	}

	
	private static int countVowelsPerWord(String word) {
		int count = 0;
		for(int i = 0; i < word.length(); i++) {
			char current_char = word.charAt(i);
			if(CountVowels.characterisvowel(current_char)) {
				count += 1;
			}
		}
		return count;
	}
	private static boolean characterisvowel(char character) {
		String vowels = "aeiou";
		boolean isvowel = false;
		if(vowels.indexOf(character) >= 0) {
			isvowel = true;
		}
		
		return isvowel;
	}
	
	private static String generatevowelreport(String text) {
		String report_text = "Here is your vowel report: \n";
		HashMap<Character, Integer> report = new HashMap<Character, Integer>();
		
		for(int i = 0; i < text.length(); i++) {
			char current_char = text.charAt(i);
			if(report.containsKey(current_char)) {
				int newnumber = report.get(current_char);
				newnumber += 1;
				report.put(current_char, newnumber);
			}
			else if(CountVowels.characterisvowel(current_char)) {
				report.put(current_char, 1);
			}
		}
		
		
		for(Map.Entry<Character, Integer> eachvowel : report.entrySet()) {
			 char vowel = eachvowel.getKey();
			 int value = eachvowel.getValue();
			 report_text += vowel +  " occurs " + value + " times \n";
		}
		return report_text;
	}
}

Question

How would you create a “vowels” report of a text file ?

Links