Hello everyone,
could someone help me please,
I want to print out the frequency of each vowel in the string and I think I will need to use the hash table I already have but im not sure how to do it
Thank you in advance
import java.util.*;
public class TextAnalyzer {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print(" Enter Text: ");
String text = in.nextLine().toLowerCase();
int vowels = 0, consonants = 0, spaces = 0, numbers = 0, punctuation = 0;
for (int i = 0; i < text.length(); i++)
{
Character c = text.charAt(i);
if (isVowel(c)) {
vowels++;
} else if (isConsonant(c)) {
consonants++;
} else if (Character.isWhitespace(c)) {
spaces++;
} else if (Character.isDigit(c)) {
numbers++;
} else if (isPunctuation(c)) {
punctuation++;
}
}
System.out.println("String length is: "+ text.length()+" characters");
if (vowels % 2 == 0 && consonants % 2 == 0)
System.out.println("The ratio of Vowels to Consonants is: " +vowels/2+ ":" +consonants/2);
else if (vowels % 3 == 0 && consonants % 3 == 0)
System.out.println("The ratio of Vowels to Consonants is: " +vowels/3+ ":" +consonants/3);
else if (vowels % 5 == 0 && consonants % 5 == 0)
System.out.println("The ratio of Vowels to Consonants is: " +vowels/5+ ":" +consonants/5);
else
System.out.println("The ratio of Vowels to Consonants is: " +vowels+ ":" +consonants);
if ((text.length() - spaces) % 2 == 0 && spaces % 2 == 0)
System.out.println("The ratio of Printing to Non-Printing is: " +(text.length()-spaces)/2+ ":" +spaces/2);
else if ((text.length() - spaces) % 3 == 0 && spaces % 3 == 0)
System.out.println("The ratio of Printing to Non-Printing is: " +(text.length()-spaces)/3+ ":" +spaces/3);
else if ((text.length() - spaces) % 5 == 0 && spaces % 5 == 0)
System.out.println("The ratio of Printing to Non-Printing is: " +(text.length()-spaces)/5+ ":" +spaces/5);
else
System.out.println("The ratio of Printing to Non-Printing is: " +(text.length()-spaces)+ ":" +spaces);
{
if (vowels >0)
{
System.out.println(" appears "+" times,which is: "+ (double)vowels * 100/(text.length())
+ "% from the whole string.");
}
}
}
private static HashSet<Character> vowels = new HashSet<Character>();
private static HashSet<Character> consonants = new HashSet<Character>();
static
{
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');
consonants.add('b');
consonants.add('c');
consonants.add('d');
consonants.add('f');
consonants.add('g');
consonants.add('h');
consonants.add('j');
consonants.add('k');
consonants.add('l');
consonants.add('m');
consonants.add('n');
consonants.add('p');
consonants.add('q');
consonants.add('r');
consonants.add('s');
consonants.add('t');
consonants.add('v');
consonants.add('w');
consonants.add('x');
consonants.add('y');
consonants.add('z');
}
private static boolean isPunctuation(Character c)
{
return c==',' || c=='.' || c=='!' || c=='?' || c=='"';
}
private static boolean isConsonant(Character c)
{
return consonants.contains(c);
}
private static boolean isVowel(Character c)
{
return vowels.contains(c);
}
}