-
Count Vowels
Hey again guys :P Sorry for needing all this help i have another question i need some help with! Thanks for any help in advance. Heres the question.
“THE QUALITY OF MERCY IS NOT STRAINED, IT DROPPETH AS THE GENTLE RAIN FROM HEAVEN ON THE PLACE BENEATH.”
This is the opening sentence of Portia’s speech in Shakespeare’s “The Merchant of Venice”.
Write a program that reads a data file containing this sentence (Use Upper case letters!) and counts the number of times each of the five vowels a, e, i, o and u occur in the sentence.
-
Welcome to DevX 
So, basically, you just need something that counts the number vowels in a sentence, right?
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
Try this
Code:
//countVowels()
int countVowels(String text) {
int count = 0; // start the count at zero
// change the entire string to uppercase
text = text.toUpperCase();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
count++;
}
}
return count;
}
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
Thanks for the starter But i need to get it to read the text from a data file.
Since there is no String args[] statement where would i tell it to read the data file?
Thanks again
-
That was just an example. Just change where the text is coming from.
You must have gone over, in your class, how to open and read from a text file, right?
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
Hey i have this code but i am getting some logic errors. The code is only reading the first word from my text file! How do i get around this? This code is probably a nightmare but i strictly cant see whats wrong with it Thanks for any help in advance!
Code:
import java.io.File;
import java.util.Scanner;
import java.io.IOException;
class Portia
{
public static void main(String args[])
throws IOException
{
Scanner PortiaScanner = new Scanner(new File("Portia.txt"));
String txt;
int vowels=0;
int letter=0;
int spaces=0;
txt = PortiaScanner.next();
System.out.println("\nThe String is :"+txt);
int n=txt.length();
for(int i=0;i<n;i++)
{
if(txt.charAt(i)=='a'||txt.charAt(i)=='A')
{
++vowels;
}
else if(txt.charAt(i)=='e'||txt.charAt(i)=='E')
{
++vowels;
}
else if(txt.charAt(i)=='i'||txt.charAt(i)=='I')
{
++vowels;
}
else if(txt.charAt(i)=='o'||txt.charAt(i)=='O')
{
++vowels;
}
else if(txt.charAt(i)=='u'||txt.charAt(i)=='U')
{
++vowels;
}
else if(txt.charAt(i)==' ')
{
++spaces;
}
else
{
++letter;
}
}
System.out.println("\nThe Vowels are :"+vowels);
System.out.println("\nThe Spaces are :"+spaces);
System.out.println("\nThe Letters are :"+letter);
}
}
Thanks people
Last edited by epilogue; 03-06-2008 at 04:01 AM.
-
What are the errors that you are getting and what lines of code are throwing them?
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
You are getting only one token from the file input ... you need to keep doing your evaluation by using a
while (PortiaScanner.hasNext() )
{
token processing code
}
loop to process each token until all the tokens are gone.
Similar Threads
-
By RichardGR in forum VB Classic
Replies: 3
Last Post: 12-28-2007, 01:08 PM
-
By corygibbons in forum Database
Replies: 6
Last Post: 07-11-2007, 08:30 PM
-
Replies: 0
Last Post: 04-20-2007, 02:13 PM
-
By barbarosa80503 in forum VB Classic
Replies: 2
Last Post: 10-28-2005, 03:33 PM
-
By peskygal in forum VB Classic
Replies: 6
Last Post: 02-22-2005, 06:47 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks