String- Vowel Count in Java
Author |
Message |
chimosfinest
|
Posted: Mon Mar 31, 2008 9:21 am Post subject: String- Vowel Count in Java |
|
|
Im taking a grade 11 programming course and i have never programmed before this semester i am working on a program that promps the user to enter a string then it counts the amount of vowels. My trouble is getting it to check each letter without having to type
if((currentLetter == a)||(currentLetter == e)||(currentLetter == i)||(currentLetter == o)||(currentLetter == u))
{
vowelCount = (vowelCount + 1);
}
So i tryed to loop it with a do-while statement which compares each character to all vowels while (charNUm < textLength) charNum is my variable for the index of substring and textLength is the total length of the inputed string. this is my program script.
//Write a program that checks whether two words are anagrams. Two words are anagrams if they contain the
//same letters in any order. For example, "silent" and "listen" are anagramsimport java.util.Scanner;
import java.util.Scanner;
public class VowelCount
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//Title
System.out.println("Vowel Count ");
//Prompt for first word\
System.out.println("Please enter the text. ");
String text = input.next();
//Declare and initialize output string
String output = "";
//Variables
int charNum = 0;
int charNumPlus =(charNum + 1);
int vowelCount= 0;
String a= ("a");
String e= ("e");
String i= ("i");
String o= ("o");
String u= ("u");
//Count
int textLength = text.length();
do
{
String currentLetter = text.substring(charNum,charNumPlus);
System.out.print(currentLetter);
//check for vowel
if((currentLetter == a)||(currentLetter == e)||(currentLetter == i)||(currentLetter == o)||(currentLetter == u))
{
vowelCount = (vowelCount + 1);
}
charNum= (charNum + 1);
}
while(textLength > charNum);
System.out.println(vowelCount);
}
}
My problem is this line is highlighted " String currentLetter = text.substring(charNum,charNumPlus);" i get these errors;
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1768)
at VowelCount.main(VowelCount.java:30)
Thanks for The help Its Appeciared |
|
|
|
|
|
Sponsor Sponsor
|
|
|
HeavenAgain
|
Posted: Mon Mar 31, 2008 10:42 am Post subject: RE:String- Vowel Count in Java |
|
|
ok as for your error, you are getting it because charNumPlus will always be 1, and so as your charNum increases to anything more than 1, you will get this error
now..... that is not a good style to code, for example, you have a variable String called a and its assigned to "a", and so on with the other letters. and you are using == to compare two strings, which is also bad, instead you should be using .equals()
also i do not recommend you to use the do...while loop structure, but instead use a for loop.
also substring can be shortened to text.charAt(i);
and also, when you have x = x + 1; it can be shortened into x++; they are the samething
another hint on how you can shorten that if statement is, you know vowels are "aeiou" so you can have a string which holds all the vowels, and use vowels.contains(text.charAt(i)); of course you have to ignore lower/upper case, i'll let you figure that part out |
|
|
|
|
|
SJ
|
Posted: Tue Apr 08, 2008 7:38 pm Post subject: RE:String- Vowel Count in Java |
|
|
you can probably half the size of your solution easily. By the way, you have a variable called String "output" that's never been really used.. why is that there? I just scanned through your code, there may be more wasted stuff.. check it through again. |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Tue Apr 08, 2008 8:42 pm Post subject: Re: String- Vowel Count in Java |
|
|
Your Java can be almost as short. (Of course, someone is going to post a much better version now...) |
|
|
|
|
|
HeavenAgain
|
Posted: Tue Apr 08, 2008 9:12 pm Post subject: RE:String- Vowel Count in Java |
|
|
like?
Java: | System. out. println(s. replaceAll("(?i)[^aeiou]", ""). length()); | altho i dont know perl, probably the samething tho, but i dont see the case sensitive part? |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Wed Apr 09, 2008 12:07 am Post subject: Re: RE:String- Vowel Count in Java |
|
|
HeavenAgain @ Tue Apr 08, 2008 9:12 pm wrote: like?
Java: | System. out. println(s. replaceAll("(?i)[^aeiou]", ""). length()); | altho i dont know perl, probably the samething tho, but i dont see the case sensitive part? Yes, lowercase only in the perl code above. |
|
|
|
|
|
|
|