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!
-
can you add the output you get?JRsz– JRsz2015年09月22日 21:19:05 +00:00Commented Sep 22, 2015 at 21:19
-
1Please 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.rajah9– rajah92015年09月22日 21:19:38 +00:00Commented 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 longestYassin Hajaj– Yassin Hajaj2015年09月22日 21:27:42 +00:00Commented Sep 22, 2015 at 21:27
-
Z = theString.length() is better. You miss a character each time otherwiseYassin Hajaj– Yassin Hajaj2015年09月22日 21:29:16 +00:00Commented 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?fge– fge2015年09月22日 21:34:59 +00:00Commented Sep 22, 2015 at 21:34
3 Answers 3
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.
-
Ha! I like that Java 8+ solution :)AndrewMcCoist– AndrewMcCoist2015年09月22日 22:13:57 +00:00Commented Sep 22, 2015 at 22:13
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.
-
how would I go about passing the longest variable into the firstlastVowel method?Ricky92d– Ricky92d2015年09月22日 21:56:06 +00:00Commented 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.) changepublic static void firstlastVowel (String theString)
topublic static void firstlastVowel (String theString, Integer longest)
in third line of your class xJavaChesser– Chesser2015年09月22日 22:18:31 +00:00Commented Sep 22, 2015 at 22:18 -
also 4.) Change the line in
main
method where you are making a call tofirstlastVowel
fromfirstlastVowel(thing);
tofirstlastVowel(thing, longest);
Chesser– Chesser2015年09月22日 22:26:30 +00:00Commented Sep 22, 2015 at 22:26
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';
}