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.
03-02-2008, 04:19 AM
Hack
Welcome to DevX :WAVE:
So, basically, you just need something that counts the number vowels in a sentence, right?
03-02-2008, 04:48 AM
Hack
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;
}
03-02-2008, 05:51 AM
epilogue
Thanks for the starter :D 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 :D
03-02-2008, 06:57 AM
Hack
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?
03-06-2008, 03:55 AM
epilogue
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 :)
03-06-2008, 06:54 AM
Hack
What are the errors that you are getting and what lines of code are throwing them?
03-06-2008, 08:20 PM
nspils
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.