Need Advice on for loops with vowels

Reply

Join Date: Sep 2004
Posts: 17
Reputation: grifflyn is an unknown quantity at this point 
Solved Threads: 0
grifflyn's Avatar
grifflyn grifflyn is offline Offline
Newbie Poster

Need Advice on for loops with vowels

 
0
  #1
Nov 5th, 2004
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");



}
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need Advice on for loops with vowels

 
0
  #2
Nov 5th, 2004
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:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 17
Reputation: grifflyn is an unknown quantity at this point 
Solved Threads: 0
grifflyn's Avatar
grifflyn grifflyn is offline Offline
Newbie Poster

Re: Need Advice on for loops with vowels

 
0
  #3
Nov 6th, 2004
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();
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need Advice on for loops with vowels

 
0
  #4
Nov 6th, 2004
>can you give me a quick explanation on what "public void print_histogram" does
It prints a histogram, like the one you wanted:
  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:
  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:
  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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 17
Reputation: grifflyn is an unknown quantity at this point 
Solved Threads: 0
grifflyn's Avatar
grifflyn grifflyn is offline Offline
Newbie Poster

Re: Need Advice on for loops with vowels

 
0
  #5
Nov 6th, 2004
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
:p
If you worry you Die.
If you dont worry you Die.
SO DONT WORRY
:cool:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need Advice on for loops with vowels

 
0
  #6
Nov 6th, 2004
>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:
  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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1
Reputation: nikkibaby1979 is an unknown quantity at this point 
Solved Threads: 0
nikkibaby1979 nikkibaby1979 is offline Offline
Newbie Poster

Whats wrong with this program

 
0
  #7
Apr 27th, 2007
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));
}
}


Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC