> How can I find the longest string in a string array?
By looping through the array and using the length() method of the String class.
Details:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
Please post these kind of questions int the New To Java-section of the forum:
http://forum.java.sun.com/forum.jspa?forumID=54
Say you had an array called stringy
String [ ] stringy;
int current = stringy[0].length();
int longest = 0;
for(int i=0;i<stringy.length;i++){
longest = Math.max(current, stringy[i])
}
for(int i=0;i<stringy.length;i++){
int l = stringy[i].length();
if(l == longest){
String longstring = stringy[i];
}
}
this will set "longest" to be the longest string then you need to loop through the array again and match this length with all the stringsn the array when they match you have found the longest string. Probably not the most elegant way of doing it but it should work.>