# JAVA longest string in array??



## huggies12345 (Oct 12, 2008)

Hi,

I am trying to find the longest word in this array.

Here is what i have up to now:


```
public class labarray {
	
	public static void main(String[] args) {
		int A [] = new int [4];
		int B [] = new int [4];
		int D [] = new int [4];
		int index;
		String word = "";
		String c [] = new String [5];
		
		c [0] = "dog";
		c [1] = "cat";
		c [2] = "fish";
		c [3] = "camel";
		c [4] = "armadillo";
		
		for (index = 0; index < c.length; index++)
		{
			
			word = c[index];
			System.out.println("The longest array element is: " + word);
			}
		}
	
}
```
This just prints out each word in the array.

I just don't know how to tell it to compare it and see which one is longer.

I want the output to say:

The longest element in the array is: armadillo

Can anyone help??


----------



## Ninjaboi (Dec 27, 2010)

Hello huggies12345!

In Java, you can use the _length()_ method to get an integer value of the size of a String data type in the _java.lang_ package. Here's an example that uses the variables you have in your application:


```
int word1Size = c [0].length();
int word2Size = c [1].length();
int word3Size = c [2].length();
int word4Size = c [3].length();
int word5Size = c [4].length();
```
Simply compare these integer values to see which one is larger and you can get the output you desire.

*Source(s):*

Java String Length, String Length Example, Java String Length Code, String Length Program
How can find the length of a string in java


----------



## huggies12345 (Oct 12, 2008)

Thanks!!


----------

