I mostly have the method done but I am confused when it comes to the If statement concerned with storing a variable with the word with the most amount of vowels, NOTE I am not allowed to use arrays. below is my code
//method for vowels and consonants
public static int Vowelcount(String sentence) {
int maxvowelcount =0;
int minvowelcount =0;
String vowel="";
int consonantcount = 0;
int vowelcount = 0;
int index = 0;
String currentword;
int spacePos;
int noOfchars = 0;
int largest = 0 ;
int smallest = 0 ;
//gets rid of leading and trailing spaces so as to get the last word
sentence=sentence.trim() + " ";
//assignments
spacePos=sentence.indexOf(" ");
currentword = sentence.substring(0, spacePos);
while (spacePos >-1)// when no spaces are found
{
noOfchars=0;
currentword = sentence.substring(0, spacePos);
// remove the first word
sentence = sentence.substring(spacePos+1);
spacePos=sentence.indexOf(" ");
// to count the number of vowels in the string
for(index = 0; index<currentword.length(); index++)
{
if(currentword.charAt(index)=='a' || currentword.charAt(index)=='e' || currentword.charAt(index)=='i' || currentword.charAt(index)=='o' ||
currentword.charAt(index)=='A' || currentword.charAt(index) == 'E' || currentword.charAt(index) == 'I' || currentword.charAt(index) == 'O' )
{
vowelcount++;
}
else
{
consonantcount++;
}
//if statement to overwrite currentword with largest/smallest
if (index == 0)
{
minvowelcount = currentword.length();
maxvowelcount = currentword.length();
}
if (vowelcount < minvowelcount)
{
minvowelcount = vowelcount;
}
if (vowelcount > maxvowelcount)
{
maxvowelcount = vowelcount;
vowel=currentword;
}
}
}
//output to show the word with largest amount of vowels
System.out.println( "word with largest amount of vowels is " + maxvowelcount);
return vowelcount;
}
Aucun commentaire:
Enregistrer un commentaire