1

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!
asked Sep 22, 2015 at 21:15
5
  • can you add the output you get? Commented Sep 22, 2015 at 21:19
  • 1
    Please consider that firstlastVowel will be called 4 times, once for each word in your String array. Longest is set to 0 each time firstlastVowel is called and it's always going to be equal to that string. Commented Sep 22, 2015 at 21:19
  • It is best to give your method the Array in parameter and loop in the method then return the longest Commented Sep 22, 2015 at 21:27
  • Z = theString.length() is better. You miss a character each time otherwise Commented Sep 22, 2015 at 21:29
  • And what is supposed to happen when two or more strings obey both criteria and have an equal length? Commented Sep 22, 2015 at 21:34

3 Answers 3

2

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.

answered Sep 22, 2015 at 22:08
1
  • Ha! I like that Java 8+ solution :) Commented Sep 22, 2015 at 22:13
0

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.

answered Sep 22, 2015 at 21:31
3
  • how would I go about passing the longest variable into the firstlastVowel method? Commented Sep 22, 2015 at 21:56
  • Ok, here are the steps - 1.) declare and initialize the longest variable in main method - Integer longest = 0; 2.) delete the declaration line from firstlastVowel method i.e. delete - int longest = 0; 3.) change public static void firstlastVowel (String theString) to public static void firstlastVowel (String theString, Integer longest) in third line of your class xJava Commented Sep 22, 2015 at 22:18
  • also 4.) Change the line in main method where you are making a call to firstlastVowel from firstlastVowel(thing); to firstlastVowel(thing, longest); Commented Sep 22, 2015 at 22:26
0

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';
}
answered Sep 22, 2015 at 22:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.