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. :)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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();
}
}
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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 );
}
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401