Results 1 to 10 of 10
  1. #1
    luscious's Avatar
    luscious is offline Member
    Join Date
    Jul 2008
    Location
    Philippines
    Posts
    10
    Rep Power
    0

    Default To find the Maximum and Minimum in an Array of Strings

    Help! I need to determine what word in the string has the minimum and maximum number of letters. So what I did is placed them in an array and tokenize it, to seperate. However, I don't know what to do after. :(

    All I know is, I should be able to make a counter for it and then compare. Here is the code:
    Java Code:
    <jsp:useBean id="groupofwords" class="common.Q1">
    	<jsp:setProperty name="groupofwords" property="sentence" value="${param.words}"/>
    </jsp:useBean>
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>The Maximum or Minimum Points</title>
    <link rel="stylesheet" href="style.css" type="text/css">
    </head>
    <body>
     
    <c:set var="str" value="${param.words}"/>
    <c:set var="delim" value=", "/>
    <c:set var="array" value="${fn:split(str, delim)}"/>
    <c:forEach var="token" items="${array}">
    		<c:out value="${token}"/>
                    <c:set var="count" value="${fn:length(token)}"/>
    </c:forEach>
    
    </body>
    </html>
    The ${param.words} came from the previous page where the user just enters the group of words.
    Last edited by luscious; 07-25-2008 at 01:08 PM. Reason: Added the code for String Length
    princess-love.net
    I'm the super girl trying to save the world.

  2. #2
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Hi,
    I have not worked with JSP but just wanted to give a small tip,
    cant you use anything like str.getLength() in JSP to get the length of the string.
    If so that will do your job.
    To finish sooner, take your own time....
    Nivedithaaaa

  3. #3
    itmani2020 is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Hi.
    Having space as a delimiter separate the string into tokens...then just go for the lenghtcount for the tokens...i think this will give u the way u want....

    Thanks,
    Mani

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    The way is clear, but how to is the problem. As Niveditha says there should be some way to find the length. Seems need someone here expertise on JSP.

  5. #5
    luscious's Avatar
    luscious is offline Member
    Join Date
    Jul 2008
    Location
    Philippines
    Posts
    10
    Rep Power
    0

    Default

    Thank you guys. I was also thinking of the same thing but unfortunately, I can't find the code earlier for the string length. Now, I have found it and the problem is now how to compare the strings in order to show which word is the maximum or minimum.

    PS, I have updated the code.
    princess-love.net
    I'm the super girl trying to save the world.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Can't you maintain two dummy variables for that. Logic should be something like this. Use two dummy variables to hold strings(actually two words). Get the first first word and set it as max length word. Find the length at the same time.

    Then get the second word. Compare it with max word(you have to deal with the length all the time). If the comparison pass it should be the max word.

    If not, check that there is a min word too. If not simply added the word. If there is a word you have to have another comparison to find the min.

    Keep going this process until the end. At the end two dummy variables holds the data you want.

    Hope it's helpful. :)

  7. #7
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    as you said
    I need to determine what word in the string has the minimum and maximum number of letters
    so

    Java Code:
    <c:set var="maxLength" value="0" />
    <c:forEach var="token" items="${array}">
    <c:set var="lengthOfString" value="${fn:length(token)}"/>
    <c:if test="${lengthOfString>maxLength}">
         <c:set var="maxLength" value="${lengthOfString}" />
         <c:set var="maxLengthString" value="${token}" />
    </c:if>
    </c:forEach>
    
    MAX LENGTH STRING : <c:out value="${maxLengthString}" />
    hope this clears ur prob.
    i am the future

  8. #8
    luscious's Avatar
    luscious is offline Member
    Join Date
    Jul 2008
    Location
    Philippines
    Posts
    10
    Rep Power
    0

    Default Thanks Rjuyal. :)

    Thanks for the help but no, it's also the same thing I have discovered earlier.

    Quote Originally Posted by rjuyal View Post
    as you said


    so

    Java Code:
    <c:set var="maxLength" value="0" />
    <c:forEach var="token" items="${array}">
    <c:set var="lengthOfString" value="${fn:length(token)}"/>
    <c:if test="${lengthOfString>maxLength}">
         <c:set var="maxLength" value="${lengthOfString}" />
         <c:set var="maxLengthString" value="${token}" />
    </c:if>
    </c:forEach>
    
    MAX LENGTH STRING : <c:out value="${maxLengthString}" />
    hope this clears ur prob.
    princess-love.net
    I'm the super girl trying to save the world.

  9. #9
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    i guess the above solution will work for the said prb. will u please let us know what kinda solution you want.
    i am the future

  10. #10
    jack239 is offline Member
    Join Date
    Jul 2008
    Posts
    35
    Rep Power
    0

    Default

    I think the code posted by rjuyal is the solution you are looking for that is if you want to find the largest string then you should compare with the previous max length. Please let us know what exception you are receiving?
    New to Java/PHP/Javascript development?
    For free help go to- www.techcubetalk.com

Similar Threads

  1. Replies: 7
    Last Post: 04-28-2008, 05:28 AM
  2. Replies: 8
    Last Post: 04-22-2008, 04:21 AM
  3. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:39 PM
  4. Replies: 1
    Last Post: 02-16-2008, 10:10 PM
  5. Maximum size of an array
    By Hasan in forum New To Java
    Replies: 1
    Last Post: 05-20-2007, 12:11 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
  •