Results 1 to 10 of 10
- 07-25-2008, 06:14 AM #1
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:
The ${param.words} came from the previous page where the user just enters the group of words.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>Last edited by luscious; 07-25-2008 at 12:08 PM. Reason: Added the code for String Length
princess-love.net
I'm the super girl trying to save the world.
- 07-25-2008, 09:42 AM #2
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
- 07-25-2008, 09:50 AM #3
Member
- Join Date
- Jun 2008
- Posts
- 23
- Rep Power
- 0
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
- 07-25-2008, 10:46 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 07-25-2008, 12:07 PM #5
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.
- 07-25-2008, 12:20 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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. :)
- 07-25-2008, 02:26 PM #7
as you said
soI need to determine what word in the string has the minimum and maximum number of letters
hope this clears ur prob.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}" />i am the future
- 07-25-2008, 02:51 PM #8
- 07-25-2008, 03:28 PM #9
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
- 07-31-2008, 01:51 PM #10
Member
- Join Date
- Jul 2008
- Posts
- 35
- Rep Power
- 0
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
-
Help with Code! Display Array of Strings and Integers - Sorted
By luvjoynt in forum New To JavaReplies: 7Last Post: 04-28-2008, 04:28 AM -
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04-22-2008, 03:21 AM -
Sorting an array of Strings
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:39 PM -
Recursive Method ==> find minimum value from array
By NatNat in forum New To JavaReplies: 1Last Post: 02-16-2008, 09:10 PM -
Maximum size of an array
By Hasan in forum New To JavaReplies: 1Last Post: 05-20-2007, 11:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks