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.prin t(" Enter Text: ");
String text = in.nextLine().t oLowerCase();
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.isWh itespace(c)) {
spaces++;
} else if (Character.isDi git(c)) {
numbers++;
} else if (isPunctuation( c)) {
punctuation++;
}
}
System.out.prin tln("String length is: "+ text.length()+" characters");
if (vowels % 2 == 0 && consonants % 2 == 0)
System.out.prin tln("The ratio of Vowels to Consonants is: " +vowels/2+ ":" +consonants/2);
else if (vowels % 3 == 0 && consonants % 3 == 0)
System.out.prin tln("The ratio of Vowels to Consonants is: " +vowels/3+ ":" +consonants/3);
else if (vowels % 5 == 0 && consonants % 5 == 0)
System.out.prin tln("The ratio of Vowels to Consonants is: " +vowels/5+ ":" +consonants/5);
else
System.out.prin tln("The ratio of Vowels to Consonants is: " +vowels+ ":" +consonants);
if ((text.length() - spaces) % 2 == 0 && spaces % 2 == 0)
System.out.prin tln("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.prin tln("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.prin tln("The ratio of Printing to Non-Printing is: " +(text.length()-spaces)/5+ ":" +spaces/5);
else
System.out.prin tln("The ratio of Printing to Non-Printing is: " +(text.length()-spaces)+ ":" +spaces);
{
if (vowels >0)
{
System.out.prin tln(" appears "+" times,which is: "+ (double)vowels * 100/(text.length())
+ "% from the whole string.");
}
}
}
private static HashSet<Charact er> vowels = new HashSet<Charact er>();
private static HashSet<Charact er> consonants = new HashSet<Charact er>();
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(C haracter c)
{
return c==',' || c=='.' || c=='!' || c=='?' || c=='"';
}
private static boolean isConsonant(Cha racter c)
{
return consonants.cont ains(c);
}
private static boolean isVowel(Charact er c)
{
return vowels.contains (c);
}
}
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.prin t(" Enter Text: ");
String text = in.nextLine().t oLowerCase();
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.isWh itespace(c)) {
spaces++;
} else if (Character.isDi git(c)) {
numbers++;
} else if (isPunctuation( c)) {
punctuation++;
}
}
System.out.prin tln("String length is: "+ text.length()+" characters");
if (vowels % 2 == 0 && consonants % 2 == 0)
System.out.prin tln("The ratio of Vowels to Consonants is: " +vowels/2+ ":" +consonants/2);
else if (vowels % 3 == 0 && consonants % 3 == 0)
System.out.prin tln("The ratio of Vowels to Consonants is: " +vowels/3+ ":" +consonants/3);
else if (vowels % 5 == 0 && consonants % 5 == 0)
System.out.prin tln("The ratio of Vowels to Consonants is: " +vowels/5+ ":" +consonants/5);
else
System.out.prin tln("The ratio of Vowels to Consonants is: " +vowels+ ":" +consonants);
if ((text.length() - spaces) % 2 == 0 && spaces % 2 == 0)
System.out.prin tln("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.prin tln("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.prin tln("The ratio of Printing to Non-Printing is: " +(text.length()-spaces)/5+ ":" +spaces/5);
else
System.out.prin tln("The ratio of Printing to Non-Printing is: " +(text.length()-spaces)+ ":" +spaces);
{
if (vowels >0)
{
System.out.prin tln(" appears "+" times,which is: "+ (double)vowels * 100/(text.length())
+ "% from the whole string.");
}
}
}
private static HashSet<Charact er> vowels = new HashSet<Charact er>();
private static HashSet<Charact er> consonants = new HashSet<Charact er>();
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(C haracter c)
{
return c==',' || c=='.' || c=='!' || c=='?' || c=='"';
}
private static boolean isConsonant(Cha racter c)
{
return consonants.cont ains(c);
}
private static boolean isVowel(Charact er c)
{
return vowels.contains (c);
}
}
Comment