944,022 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5162
  • Java RSS
Nov 5th, 2004
0

Need Advice on for loops with vowels

Expand Post »
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");



}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grifflyn is offline Offline
17 posts
since Sep 2004
Nov 5th, 2004
0

Re: Need Advice on for loops with vowels

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:
Java Syntax (Toggle Plain Text)
  1. public void print_histogram ( char vowel, int frequency ) {
  2. System.out.print ( vowel + ": " );
  3. while ( --frequency >= 0 )
  4. System.out.print ( "*" );
  5. System.out.println();
  6. }
Call print_histogram for each vowel with the counter as the second argument, and you're sorted.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 6th, 2004
0

Re: Need Advice on for loops with vowels

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();
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grifflyn is offline Offline
17 posts
since Sep 2004
Nov 6th, 2004
0

Re: Need Advice on for loops with vowels

>can you give me a quick explanation on what "public void print_histogram" does
It prints a histogram, like the one you wanted:
Java Syntax (Toggle Plain Text)
  1. print_histogram ( 'A', 5 );
  2. print_histogram ( 'E', 3 );
  3. print_histogram ( 'I', 3 );
  4. print_histogram ( 'O', 4 );
  5. print_histogram ( 'U', 1 );
Would result in the following:
Java Syntax (Toggle Plain Text)
  1. A: *****
  2. E: ***
  3. I: ***
  4. O: ****
  5. 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:
Java Syntax (Toggle Plain Text)
  1. /*
  2.   Description: Simple vowel histogram program
  3.   Author: Julienne (Narue) Guth
  4.   Creation: 20041106
  5. */
  6. package scratchpad;
  7.  
  8. import java.util.*;
  9.  
  10. public class Main {
  11. public static void main ( String[] args ) {
  12. final String vowels = "AEIOU";
  13.  
  14. Scanner in = new Scanner ( System.in );
  15. int[] frequency = new int[vowels.length()];
  16. String line;
  17.  
  18. System.out.print ( "Enter a line: " );
  19. line = (in.nextLine()).toUpperCase();
  20.  
  21. for ( int i = 0; i < line.length(); i++ ) {
  22. int match = vowels.indexOf ( line.charAt ( i ) );
  23.  
  24. if ( match != -1 )
  25. ++frequency[match];
  26. }
  27.  
  28. for ( int i = 0; i < vowels.length(); i++ ) {
  29. System.out.print ( vowels.charAt ( i ) + ": " );
  30. for ( int j = 0; j < frequency[i]; j++ )
  31. System.out.print ( "*" );
  32. System.out.println();
  33. }
  34. }
  35. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 6th, 2004
0

Re: Need Advice on for loops with vowels

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grifflyn is offline Offline
17 posts
since Sep 2004
Nov 6th, 2004
0

Re: Need Advice on for loops with vowels

>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:
Java Syntax (Toggle Plain Text)
  1. /*
  2.   Description: Simple vowel histogram program
  3.   Author: Julienne (Narue) Guth
  4.   Creation: 20041106
  5. */
  6. package scratchpad;
  7.  
  8. import java.io.*;
  9.  
  10. public class Main {
  11. public static void print_histogram ( final char ch, int freq ) {
  12. System.out.print ( ch + ": " );
  13. for ( int i = 0; i < freq; i++ )
  14. System.out.print ( "*" );
  15. System.out.println();
  16. }
  17.  
  18. public static void main ( String[] args ) throws IOException {
  19. int ca, ce, ci, co, cu;
  20. char ch;
  21.  
  22. ca = ce = ci = co = cu = 0;
  23. while ( ( ch = (char)System.in.read() ) != -1 && ch != '\n' ) {
  24. ch = Character.toUpperCase ( ch );
  25.  
  26. if ( ch == 'A' )
  27. ++ca;
  28. else if ( ch == 'E' )
  29. ++ce;
  30. else if ( ch == 'I' )
  31. ++ci;
  32. else if ( ch == 'O' )
  33. ++co;
  34. else if ( ch == 'U' )
  35. ++cu;
  36. }
  37.  
  38. print_histogram ( 'A', ca );
  39. print_histogram ( 'E', ce );
  40. print_histogram ( 'I', ci );
  41. print_histogram ( 'O', co );
  42. print_histogram ( 'U', cu );
  43. }
  44. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 27th, 2007
0

Whats wrong with this program

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


Reputation Points: 10
Solved Threads: 0
Newbie Poster
nikkibaby1979 is offline Offline
1 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help Needed
Next Thread in Java Forum Timeline: Problem With Hibernate.cfg.xml File





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC