| | |
Need Advice on for loops with vowels
![]() |
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");
}
}
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:
Call print_histogram for each vowel with the counter as the second argument, and you're sorted.
Java Syntax (Toggle Plain Text)
public void print_histogram ( char vowel, int frequency ) { System.out.print ( vowel + ": " ); while ( --frequency >= 0 ) System.out.print ( "*" ); System.out.println(); }
I'm here to prove you wrong.
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();
}
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:
Would result in the following:
My choice of putting the operations inside of a method is irrelevant, you can put everything in one big main method if you want:
It prints a histogram, like the one you wanted:
Java Syntax (Toggle Plain Text)
print_histogram ( 'A', 5 ); print_histogram ( 'E', 3 ); print_histogram ( 'I', 3 ); print_histogram ( 'O', 4 ); print_histogram ( 'U', 1 );
Java Syntax (Toggle Plain Text)
A: ***** E: *** I: *** O: **** U: *
Java Syntax (Toggle Plain Text)
/* 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(); } } }
I'm here to prove you wrong.
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
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:
If you worry you Die.
If you dont worry you Die.
SO DONT WORRY
:cool:
>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:
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)
/* 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 ); } }
I'm here to prove you wrong.
•
•
Join Date: Apr 2007
Posts: 1
Reputation:
Solved Threads: 0
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));
}
}
// 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));
}
}
![]() |
Similar Threads
- displaying data from a sorted array (C++)
- need advice on nested loops. (PHP)
- Loops in Python (Python)
- Nested For Loops (C++)
Other Threads in the Java Forum
- Previous Thread: Help Needed
- Next Thread: Problem With Hibernate.cfg.xml File
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows






