Hash table problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NoOneCanHelpMe
    New Member
    • Aug 2008
    • 13

    Hash table problem

    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);
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Where is the Hashtable in your code?

    Comment

    • NoOneCanHelpMe
      New Member
      • Aug 2008
      • 13

      #3
      Originally posted by r035198x
      Where is the Hashtable in your code?
      ops the wrong code, but nvm I've just read that my hashtable is rubbish and I deleted it :(
      Anyway, any suggestions how to make my program to printout the frequency of each vowel?
      Thank you

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Where did you read that it's rubbish?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by NoOneCanHelpMe
          Anyway, any suggestions how to make my program to printout the frequency of each vowel?
          Thank you
          Why don't you use regular expressions for that. Because the following is a
          complete giveaway there is no explanation. Read the API documentation for
          the relevant classes and methods:

          [code=java]
          int frequencyOf(Str ing s, String pattern) {
          return s.length()-s.replaceAll(pa ttern, "").length( );
          }
          [/code]

          Call that method with your text for the first parameter and, say, "[aA]" for its
          second parameter. It'll return the number of lowercase or uppercase letters 'a'
          in your text. If the second parameter is "[aAeEiIoOuU]" it returns the count of
          any vowel in your text.

          kind regards,

          Jos

          ps. you already have a thread running for exactly the same question. Don't double
          post; it is confusing for readers who are willing to help you.

          Comment

          • NoOneCanHelpMe
            New Member
            • Aug 2008
            • 13

            #6
            thank you for your help :)

            Comment

            Working...