Returning the element number of the longest string in an array

Posted by JohnRoberts on Stack Overflow See other posts from Stack Overflow or by JohnRoberts
Published on 2010-06-14T03:08:20Z Indexed on 2010/06/14 3:22 UTC
Read the original article Hit count: 293

Filed under:
|
|
|

I'm trying to get the longestS method to take the user-inputted array of strings, then return the element number of the longest string in that array. I got it to the point where I was able to return the number of chars in the longest string, but I don't believe that will work for what I need. My problem is that I keep getting incompatible type errors when trying to figure this out. I don't understand the whole data type thing with strings yet. It's confusing me how I go about return a number of the array yet the array is of strings. The main method is fine, I got stuck on the ???? part.

{
  public static void main(String [] args) 
  {
    Scanner inp = new Scanner( System.in );
    String [] responseArr= new String[4];

for (int i=0; i<4; i++)
{
  System.out.println("Enter string "+(i+1));
  responseArr[i] = inp.nextLine();
}

    int highest=longestS(responseArr);

  }

  public static int longestS(String[] values)
  {       
    int largest=0

    for( int i = 1; i < values.length; i++ )
    {
      if ( ?????          ) 
    }

return largest; 
   }

 }

© Stack Overflow or respective owner

Related posts about java

Related posts about string