Results 1 to 6 of 6
Thread: Longest word in a program...
- 08-20-2010, 11:38 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 3
- Rep Power
- 0
Longest word in a program...
hi...
i have here an example of a java program that finds the longest string in a String array...
any ideas on how to change this program to find the longest word in a sentence inputted by the user??Java Code: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(indexMaxLength); } }
a simple help would be great...
- 08-20-2010, 12:04 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Have a look at the String.split( ... ) method; you can split the String on something sensible (e.g. one or more spaces, i.e. \\s+) and you have got your array back.
kind regards,
Jos
- 08-20-2010, 12:35 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 3
- Rep Power
- 0
aww..i don't have any idea how to use the String.split()...:confused:
thanks for your reply sir...
- 08-20-2010, 12:44 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 08-20-2010, 01:08 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 3
- Rep Power
- 0
is string.spilt the only way??
'cause our prof told us that the coverage of this problem is "objects and input/output" and 'control structures"...i can't find string.split in those coverages...
- 08-20-2010, 01:34 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Of course using the String.split( ... ) method isn't the only way; you can always do it yourself; you have to use a clever loop that finds the first non-space character in the String and the first space character to the right of it. You can use the String.substring( ... ) method to chop that part from the String (you found a word) and continue to the right of that space character and repeat your loop, but I find it a bit mechanical at best ...
kind regards,
Jos
Similar Threads
-
java longest word in array
By mayhewj7 in forum New To JavaReplies: 10Last Post: 04-24-2009, 01:39 AM -
Word
By right2001 in forum New To JavaReplies: 2Last Post: 04-07-2009, 03:25 AM -
Word Jumbler
By wethekings in forum New To JavaReplies: 7Last Post: 02-20-2009, 04:57 AM -
Word Frequency
By capu in forum Advanced JavaReplies: 2Last Post: 10-09-2008, 02:03 PM -
Word OLE
By Java Tip in forum SWTReplies: 0Last Post: 07-25-2008, 02:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks