Results 1 to 6 of 6
  1. #1
    hustlas4ever is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default Longest word in a program...

    hi...

    i have here an example of a java program that finds the longest string in a String array...

    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);
          }
       }
    any ideas on how to change this program to find the longest word in a sentence inputted by the user??

    a simple help would be great...

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    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

  3. #3
    hustlas4ever is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    aww..i don't have any idea how to use the String.split()...:confused:

    thanks for your reply sir...

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hustlas4ever View Post
    aww..i don't have any idea how to use the String.split()...:confused:

    thanks for your reply sir...
    Try this:

    Java Code:
    String[] yourArray= yourString.split("\\s+");
    ... and read the API documentation for that method.

    kind regards,

    Jos

  5. #5
    hustlas4ever is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    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...

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hustlas4ever View Post
    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...
    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

  1. java longest word in array
    By mayhewj7 in forum New To Java
    Replies: 10
    Last Post: 04-24-2009, 02:39 AM
  2. Word
    By right2001 in forum New To Java
    Replies: 2
    Last Post: 04-07-2009, 04:25 AM
  3. Word Jumbler
    By wethekings in forum New To Java
    Replies: 7
    Last Post: 02-20-2009, 05:57 AM
  4. Word Frequency
    By capu in forum Advanced Java
    Replies: 2
    Last Post: 10-09-2008, 03:03 PM
  5. Word OLE
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 03:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •