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");


}
}

Dani AI

Generated

The "; expected" error you saw usually means a method declaration ended up inside another method (for example you pasted public void print_histogram(...) inside main). In Java, methods must be declared at class level, not nested inside other methods. Also remember: public is an access modifier, void means "no return value", and if you want to call a helper method from static main either make that helper static or call it on a new instance of the class. As hinted, keeping a separate counter for each vowel is the simplest idea.

If you want to stick to only for/while and charAt (no extra methods or arrays), the steps are:

  • Read the line.
  • Walk the line with a for loop and charAt.
  • For each char compare its lowercase form, increment the appropriate vowel counter, and append an uppercase vowel (or the original char) to a StringBuilder.
  • After the loop print the transformed sentence, then print each vowel label followed by a simple for loop that prints * for the count.

A minimal example (keeps everything inside main so you can see where to place code):

import java.io.*;

public class VowelCounterSimple {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter a sentence: ");
    String input = br.readLine();

    int countA=0, countE=0, countI=0, countO=0, countU=0;
    StringBuilder out = new StringBuilder(input.length());

    for (int k=0; k<input.length(); k++) {
      char ch = input.charAt(k);
      char low = Character.toLowerCase(ch);
      if (low == 'a') { countA++; out.append('A'); }
      else if (low == 'e') { countE++; out.append('E'); }
      else if (low == 'i') { countI++; out.append('I'); }
      else if (low == 'o') { countO++; out.append('O'); }
      else if (low == 'u') { countU++; out.append('U'); }
      else out.append(ch);
    }

    System.out.println(out.toString());
    System.out.print("A: "); for (int i=0;i<countA;i++) System.out.print('*'); System.out.println();
    System.out.print("E: "); for (int i=0;i<countE;i++) System.out.print('*'); System.out.println();
    System.out.print("I: "); for (int i=0;i<countI;i++) System.out.print('*'); System.out.println();
    System.out.print("O: "); for (int i=0;i<countO;i++) System.out.print('*'); System.out.println();
    System.out.print("U: "); for (int i=0;i<countU;i++) System.out.print('*'); System.out.println();
  }
}

Quick tips: avoid the while (--freq >= 0) trick until you're comfortable with pre-decrement (it mutates the parameter). Test with mixed case and punctuation. If you later want to tidy code, move the star-printing into a small static method placed at class level (not inside main) so you can reuse it. This keeps the program clear for exams and practice.

Recommended Answers

All 6 Replies

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.