how can I find the longest String?

how can I find the longest string in a String array?
[59 byte] By [bonoza] at [2007-10-2 18:17:20]
# 1
By comparing their lengths.
CeciNEstPasUnProgrammeura at 2007-7-13 19:37:36 > top of Java-index,Java Essentials,Java Programming...
# 2
@OP: don't crosspost, please.
prometheuzza at 2007-7-13 19:37:36 > top of Java-index,Java Essentials,Java Programming...
# 3
sorry.--sure I can produce each string's length. How do I compare each and everyone of the string with the each other?compareTo()?
bonoza at 2007-7-13 19:37:36 > top of Java-index,Java Essentials,Java Programming...
# 4
> sorry.> --> > sure I can produce each string's length. How do I> compare each and everyone of the string with the each> other?> > compareTo()?I answered in your other thread.
prometheuzza at 2007-7-13 19:37:36 > top of Java-index,Java Essentials,Java Programming...
# 5
Alright. the length will tell me the lengths of each of those strings.But how can compare those lengths with each other and be returned with a result that tells me that mystring[2] is the largest string?
bonoza at 2007-7-13 19:37:36 > top of Java-index,Java Essentials,Java Programming...
# 6

> Alright.

>

> the length will tell me the lengths of each of those

> strings.

>

> But how can compare those lengths with each other and

> be returned with a result that tells me that

> mystring[2] is the largest string?

With some if-statements: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html

Please try it yourself first. If you run into problems, post your code here (using code tags*) and explain what the problem is.

* http://forum.java.sun.com/help.jspa?sec=formatting

prometheuzza at 2007-7-13 19:37:36 > top of Java-index,Java Essentials,Java Programming...
# 7
- indexMaxLength = 0- for each String-- if length > str[indexMaxLength].length indexMaxLength = iSix lines of code, one iteration.
CeciNEstPasUnProgrammeura at 2007-7-13 19:37:36 > top of Java-index,Java Essentials,Java Programming...
# 8
By the way, this is not the place to teach you programming. If you have difficulties transforming problems into code, you should get a personal tutor. Those discussions and explanations can become lengthy and very tiring over a forum.
CeciNEstPasUnProgrammeura at 2007-7-13 19:37:36 > top of Java-index,Java Essentials,Java Programming...
# 9

I gave it a go, and what I came up with didn't quite work. Can you guys find what i'm missing here thanks.

public class StringTest

{

public static void main (String [] args)

{

String[] k = new String[10];

int indexMaxLength = 0;

int i;

k[0] = "asdfasdfasdfasdfasdfasdfasdfasdf";

k[1] = "asdfasdf";

k[2] = "asdfasdfasdfasdfasdfasdfasdfasdfasdfasdf";

k[3] = "asdfasdfasdfasdf";

k[4] = "asdfasdfasdfasdfasdf";

k[5] = "asdfasdfasdfasdfasdfasdf";

k[6] = "adsdf";

k[7] = "adsdf";

k[8] = "asdfasdfasdfasdfasdfasdfasdfasdfasdf";

k[9] = "asdfasdfasdfasdfasdfasdfasdf";

for(i = 0; i < k.length; i++)

{

if(k[i].length() > k[indexMaxLength].length())

{

indexMaxLength = i;

}

}

System.out.println(i);

}

}

bonoza at 2007-7-13 19:37:36 > top of Java-index,Java Essentials,Java Programming...
# 10
It did work - print out indexMaxLength, not i.
by_pedda at 2007-7-13 19:37:37 > top of Java-index,Java Essentials,Java Programming...
# 11

you're right, it works now.

'I kind of get how it's working.

But if someone doesn't mind explaining what's going on, can you explain what's going on?

I see that as i is incrementing, it is being compared to the length of the string at 0, and if it is greater than that, then that 0 becomes whatever i is. But still confuses me..

bonoza at 2007-7-13 19:37:37 > top of Java-index,Java Essentials,Java Programming...
# 12
Nevermind guys, I get it completely.Thanks for all your help :-)
bonoza at 2007-7-13 19:37:37 > top of Java-index,Java Essentials,Java Programming...
# 13

You're the one who wrote it. Kind of odd that you'd want others to explain it to you.

Personally I would have done it this way:

String[] stuff = // do something to fill stuff with Strings of varying length

// and for simplicity assume length of the array > 1, and no position in the array is null

int longest = 0;

for (int i = 1; i < stuff.length; i++)

if (stuff[i].length() > stuff[longest].length())

longest = i;

paulcwa at 2007-7-13 19:37:37 > top of Java-index,Java Essentials,Java Programming...
# 14

> Personally I would have done it this way:

> > String[] stuff = // do something to fill stuff with

> Strings of varying length

> // and for simplicity assume length of the array > 1,

> and no position in the array is null

> int longest = 0;

> for (int i = 1; i < stuff.length; i++)

>if (stuff[i].length() > stuff[longest].length())

>longest = i;

>

I'm sorry, I don't see the difference - are you suggesting your version for readability? Because I think that comes way after understanding the fundamentals behind the code...

by_pedda at 2007-7-13 19:37:37 > top of Java-index,Java Essentials,Java Programming...
# 15

> I'm sorry, I don't see the difference -

I don't see the difference anymore either. I think I had somehow confused two different threads. Apologizes for any confusion.

> are you suggesting your version for readability? Because I

> think that comes way after understanding the

> fundamentals behind the code...

I'm not convinced there's a difference between readability and understanding the fundamental structure and algorithm in the code.

paulcwa at 2007-7-20 23:37:58 > top of Java-index,Java Essentials,Java Programming...