Hi I have written this program to find out how many vowels are in a sentance.
What i need to do now is is capitalise the vowels and put a star next to the capital showing how many times that vowel was used in the sentance.

so that the output is like this

A: ***** (if there were 5 A's)
E: *** (if ther were 3 E's)
I:  ***
O: ****
U: *

any help and advice woud be much appreciated

// filename VowelCounter.java
//created Fri 5th nov 2004
// counts vowels in a sentance


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;


public class VowelCounter


{
public static void main(final String[] pArgs) throws IOException
{


final InputStreamReader tInputStreamReader = new InputStreamReader(System.in);
final BufferedReader tKeyboard = new BufferedReader(tInputStreamReader);


System.out.println(" this will count how many vowels are in a sentance");
System.out.println(" Please enter a Short sentance");
final String tVowel = tKeyboard.readLine();
final int tVowelCount = tVowel.length();
//begin for loop
for (int tVowelNumber =0; tVowelNumber<tVowelCount; tVowelNumber++)
{
final char tVow = tVowel.charAt(tVowelNumber);
final char tLowerVow = Character.toLowerCase(tVow);


// start of if statement
if (tLowerVow=='a' || tLowerVow=='e' || tLowerVow=='i' || tLowerVow=='o' || tLowerVow=='u')
{
System.out.print(tVow);
System.out.println();
}
// end if statement


}
//end for loop
System.out.println(" These are the vowels  that are in the sentance");


}
}

The simple solution is to keep a counter for each vowel. When you encounter that vowel, increment the counter. Then at the end you can do something like this:

public void print_histogram ( char vowel, int frequency ) {
  System.out.print ( vowel + ": " );
  while ( --frequency >= 0 )
    System.out.print ( "*" );
  System.out.println();
}

Call print_histogram for each vowel with the counter as the second argument, and you're sorted. :)

Hi Narue,
Thanks for the reply can you give me a quick explanation on what "public void print_histogram" does. We havent been shown this in our lectures and i arent sure how it works. I am finding all this Java stuff overwhelming along with the rest of the subjects i am doing.
Is there and way that i can do the program without using "Public histogram" and just using "for", "while" and "do" loops with "charAt"


public void print_histogram ( char vowel, int frequency )
{
System.out.print ( vowel + ": " );
while ( --frequency >= 0 )
System.out.print ( "*" );
System.out.println();
}

>can you give me a quick explanation on what "public void print_histogram" does
It prints a histogram, like the one you wanted:

print_histogram ( 'A', 5 );
print_histogram ( 'E', 3 );
print_histogram ( 'I', 3 );
print_histogram ( 'O', 4 );
print_histogram ( 'U', 1 );

Would result in the following:

A: *****
E: ***
I: *** 
O: ****
U: *

My choice of putting the operations inside of a method is irrelevant, you can put everything in one big main method if you want:

/*
  Description: Simple vowel histogram program
  Author:      Julienne (Narue) Guth
  Creation:    20041106
*/
package scratchpad;

import java.util.*;

public class Main {
  public static void main ( String[] args ) {
    final String vowels = "AEIOU";
    
    Scanner in = new Scanner ( System.in );
    int[] frequency = new int[vowels.length()];
    String line;
    
    System.out.print ( "Enter a line: " );
    line = (in.nextLine()).toUpperCase();
    
    for ( int i = 0; i < line.length(); i++ ) {
      int match = vowels.indexOf ( line.charAt ( i ) );
      
      if ( match != -1 )
        ++frequency[match];
    }
    
    for ( int i = 0; i < vowels.length(); i++ ) {
      System.out.print ( vowels.charAt ( i ) + ": " );
      for ( int j = 0; j < frequency[i]; j++ )
        System.out.print ( "*" );
      System.out.println();
    }
  }
}

Hi Narue,
Thanks for the help it still don't make sense to me and when i tried to put the "public void print_histogram" line in i am now getting the following error msg

"C:\Documents and Settings\Administrator\My Documents\A School Documents\Programming\week8\VowelCounter.java:14: ';' expected
public void print_histogram
^
1 error"
The code you used isn't anything like what we have been taught as of yet. I am just hoping that the language will sink in eventualy. Like within the next 3 weeks as i have my first exam then LOL
I doubt that i can blag it if i am stuggling with simple things like histograms

thanks for your help anyway i guess i need to read my book a bit more

>i am now getting the following error msg
Then you're not using the function correctly.

>The code you used isn't anything like what we have been taught as of yet.
Well, since you haven't mentioned your level of experience I have to make some assumptions or dumb down my code so much as to risk insulting you. Here, this doesn't use Strings or arrays, but it's significantly larger even when the histogram method is used:

/*
  Description: Simple vowel histogram program
  Author:      Julienne (Narue) Guth
  Creation:    20041106
*/
package scratchpad;

import java.io.*;

public class Main {
  public static void print_histogram ( final char ch, int freq ) {
    System.out.print ( ch + ": " );
    for ( int i = 0; i < freq; i++ )
      System.out.print ( "*" );
    System.out.println();
  }
  
  public static void main ( String[] args ) throws IOException {
    int ca, ce, ci, co, cu;
    char ch;
    
    ca = ce = ci = co = cu = 0;
    while ( ( ch = (char)System.in.read() ) != -1 && ch != '\n' ) {
      ch = Character.toUpperCase ( ch );
      
      if ( ch == 'A' )
        ++ca;
      else if ( ch == 'E' )
        ++ce;
      else if ( ch == 'I' )
        ++ci;
      else if ( ch == 'O' )
        ++co;
      else if ( ch == 'U' )
        ++cu;
    }
    
    print_histogram ( 'A', ca );
    print_histogram ( 'E', ce );
    print_histogram ( 'I', ci );
    print_histogram ( 'O', co );
    print_histogram ( 'U', cu );
  }
}

public class DebugTwo4
// This program calculates tuition bills
{
public static void main(String args[])
{
int myCredits = 15;
double yourCredits = 16.5;
double rate = 75.84;
tuitionBill(myCredits,rate);
tuitionBill(yourCredits,rate);
}
public static void tuitionBill(c,r)
{
System.out.println("Total due " + (r*c));
}
public static void tuitionBill(c, r)
{
System.out.println("Total due " + (r*c));
}
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.