The problem i am working on now states:
A method named vowels that accepts a String array named alpha. The method will return the index of the
String that contains the most vowels. If there is a tie anyone will be good.
my method i have:
and to run it i have:Code:public class ArrayMethods2 { public int vowels (String[] alpha){ int[] count = new int[alpha.length]; int max = 0; for(int i = 0; i <alpha.length; i++){ //goes through the array to find each string String temp = alpha[i]; //stores the string in a temporary place temp = temp.toLowerCase(); //changes the string to lower case int vowelCount = 0; //resets sum of vowels to zero for(int j =0; j < temp.length(); j++){ //goes through the string to find a vowel if(temp.charAt(j) == 'a' || temp.charAt(j) == 'e' || temp.charAt(j) == 'i' || temp.charAt(j) == 'o' || temp.charAt(j) == 'u'){ count[i] = vowelCount++; //stores the amount of vowels for each string at each index } } } //goes through the count array to find which index had the most vowels for(int i = 0; i<count.length; i++){ if(count[i] > max) max = i; else if(count[i] == max) max = i; } return max; }
this is what i get as a result:Code:import java.util.Scanner; public class RunArray2{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayMethods2 am2 = new ArrayMethods2(); System.out.println("\nMethod \"vowels"+ "\""); System.out.println("How many words will you like to input?"); int amount = scan.nextInt(); String[] words = new String[amount]; System.out.println("Input your words."); for (int i = 0; i < words.length; i++) words[i] = scan.next(); System.out.println("The word at index " + am2.vowels(words) + " has the most vowels" ); } }
it should be the word at index 0Code:Method "vowels" How many words will you like to input? 3 Input your words. yankee can arm The word at index 1 has the most vowels
can someone advise me on what i did wrong?
if i move yankee to index 1 or 2 it'll give me the right answer.
also if i put in 2 strings for example index 0 = can, index 1 = j. It'll tell me index 0 has the most vowels




Reply With Quote
