Ricky92d
Ricky92d

Reputation: 171

java - Longest string in array containing vowel at start and the end

I am trying to get the longest string in an array that starts and ends with a vowel. When I run my code the longest value is displayed after each loop, but it does not display the highest value for the variable longest.

class xJava
{
public static void firstlastVowel (String theString)
{
    int index;
    int longest=0;
    char x = theString.charAt(index=0);
    char y = theString.charAt(theString.length()-1);
    int z = theString.length()-1;

    if(x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')
    {
        if(y == 'a' || y == 'e' || y == 'i' || y == 'o' || y == 'u')
        {
            System.out.println(theString + " starts and ends with a vowel");

            if(z > longest)
            {
                longest = z;
                System.out.println("longest string is "+longest+" characters!");
            }
        }


    }
}



public static void main (String[] args)
{
    int index;
    String value;

    String[] things = {"abba", "orlando", "academia", "ape"};


    for(String thing : things)
    {
        firstlastVowel(thing);
    }

}

}

how can I get the variable longest to only contain the length of the longest string?

output is :

 abba starts and ends with a vowel
 longest string is 3 characters!
 orlando starts and ends with a vowel
 longest string is 6 characters!
 academia starts and ends with a vowel
 longest string is 7 characters!
 ape starts and ends with a vowel
 longest string is 2 characters!

Upvotes: 1

Views: 1051

Answers (3)

AndrewMcCoist
AndrewMcCoist

Reputation: 639

This might be a simple example of what you need:

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    String[] things = {"abba", "orlando", "academia", "ape", "nebuchadnezzar", "academi","academian"};
    String longestWord = getLongestVowelWord(things);
    System.out.println("The longest vowel word is: " + longestWord);
}

/**
 * Method returning the String which starts and ends with a vowel and is the longest
 * within the Strings given as an array in this method's parameter
 * @param words String array containing words to be examined by the method
 * @return String object which starts and ends with a vowel and is the longest from
 * the words given as a String array
 */
private static String getLongestVowelWord(String[] words){
    String longestWord = "";
    for(String word : words){
        if(longestWord.length() < getVowelWordLength(word)){
            longestWord = word;
        }
    }
    return longestWord;
}

/**
 * Method check if word starts and ends with a vowel
 * and returning it's length it case it matches the pattern
 * @param word a word whose length is being checked on condition that it starts and ends with a vowel
 * @return if word starts and ends with a vowel, returns it's lenght. Returns -1 otherwise.
 */
private static Integer getVowelWordLength(String word){
    // check if first and last char of the word is a vowel
    // toLowerCase() is used for simplicity of isVowel(char c) method - it does not have to check the upper case chars
    if(isVowel(word.toLowerCase().charAt(0)) 
                && isVowel(word.toLowerCase().charAt(word.length()-1)))
        return word.length();
    else
        return -1;
}

/**
 * Method checking if given char is a vowel
 * @param c char being checked for being a vowel
 * @return <code>true</code> if given char is a vowel, <code>false</code> otherwise
 */
private static boolean isVowel(char c){
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}

Upvotes: 0

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

Gah, I should know better than to post this:

  String[] things = { "aa", "orrro", "academia", "ape" };
  int longest = Arrays.stream(things)
      .filter(s -> s.matches("^[aeiouy].*[aeiouy]$"))
      .map(String::length)
      .reduce(0, Math::max);
  System.out.println("longest string is " + longest + " characters!");

Why and how that works is left as an exercise for the reader.

Upvotes: 2

Chesser
Chesser

Reputation: 465

Here is the problem -

Your longest variable is method scoped in firstlastVowel and is reset to zero every time you make a call to firstlastVowel method.

Initialize it in the main method and pass it as a parameter to firstlastVowel along with the thing and use Integer type instead of int in order to keep the reference between the multiple passes of this variable to the firstlastVowel method.

Upvotes: 0

Related Questions