?
Solved

Parsing a textarea

Posted on 2003-03-12
7
Medium Priority
?
1,600 Views
Last Modified: 2012-06-27
Hi,
I am trying to use a textarea to get a list of numbers from the user:

123
456

When the page is posted, I want to parse the textarea into, in this case, two strings, 123 and 456.  This is how I am trying to do it (txtaPNs is the name of the textarea):

<c:out value="${param.txtaPNs}" />

<c:forTokens items="${param.txtaPNs}" delims="\r\n" var="partNum">
  <br />  <c:out value="${partNum}" /> x
</c:forTokens>

<br /> done

This is what I get:

123 456
123 456 x
done

I've tried all kinds of things to detect the newline(?) between 123 & 456 but nothing has worked.  How should I do this?  I would prefer using the JSTL but I can deal with any of the alternatives.

Thanks,
Eric
Tomcat 4.1.17 and latest JSTL
0
Comment
Question by:EricE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 8125953
<%
   java.util.StringTokenizer st = new java.util.StringTokenizer(text, "\n");
   String a = st.nextToken();
   String b = st.nextToken();
%>
0
 
LVL 92

Expert Comment

by:objects
ID: 8125959
oops, probably want to also add \r in the delimiter string.
0
 
LVL 28

Expert Comment

by:rrz
ID: 8128780
Eric, your JSTL tags are fine. Just add <pre> tags.
<c:forTokens items="${param.txtaPNs}" delims="\r\n" var="partNum">
 <br />  <pre><c:out value="${partNum}" /> x</pre>
</c:forTokens>
Works for me. In fact
<pre><c:out value="${param.txtaPNs}" /></pre>
works just as well.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:EricE
ID: 8134396
Hi,
rrz - Something strange here...  I tried your suggestion and it worked in that the 123 and 456 were displayed on separate lines.  However, what I was actually doing was trying to check how forTokens works by printing to the screen but what I really want to do is build up an SQL query string.  When I tried doing that under the assumption that the forTokens was working I found that it wasn't.  Apparently the <pre> tag (which I don't really understand) was just embedding a crlf in the display page.

Here is where I am trying to build up a query string (param.partNums is passed from a previous page as a param, it is the contents of the textarea):

<br /><c:out value="${param.partNums}" /> (first c:out)
<br />
<c:set value="IN(" var="srchPartNums" />

<c:forTokens items="${param.partNums}" delims="\r\n" var="partNum" varStatus="status">

  <c:set value="${srchPartNums}${partNum}," var="srchPartNums" />
  <pre><c:out value="${partNum}" /></pre> <c:out value="${status.count}" />

</c:forTokens>

<c:set value="${srchPartNums})" var="srchPartNums" />
<br />
<pre><c:out value="${srchPartNums}" /></pre>
<br /><c:out value="${srchPartNums}" />

What I get is:

123 456 (first c:out)

123
456
1

IN(123
456,)
IN(123 456,)

Sooo...still seeking the answer.  Sorry for the possible misdirection on what I was trying to do.

objects - I will have to try your approach tomorrow, gotta run now

Thanks,
Eric
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 8134485
<%
  java.util.StringTokenizer st = new java.util.StringTokenizer(text, "\n\r");
  String a = st.nextToken();
  String b = st.nextToken();
  String in = "IN("+a+","+b+")";
%>

of if you don't have set # of tokens:

<%
  String in = "IN(";
  java.util.StringTokenizer st = new java.util.StringTokenizer(text, "\n\r");
  while (st.hasMoreTokens())
  {
    in += st.nextToken();
    if (st.hasMoreTokens()) in += ",";
  }
  in += ")";
%>

0
 

Author Comment

by:EricE
ID: 8141517
Hi,
I guess I'll declare victory though it seems like there should be a way to make forTokens work.

I did have to make a slight modification so that I could make the input string visible to the scriptlet, here is what I ended up with:

<c:set value="${param.partNums}" var="partNumCol" />
<%
  String srchPartNums = (String) pageContext.getAttribute("partNumCol");
  String srchString = "IN(";
  java.util.StringTokenizer st = new java.util.StringTokenizer(srchPartNums, "\n\r");
  while (st.hasMoreTokens())
  {
    srchString += st.nextToken();
    if (st.hasMoreTokens()) srchString += ",";
  }
  srchString += ")";
  pageContext.setAttribute("srchString",srchString);
%>
<br /> <c:out value="${srchString}" />

Output was:  IN(123,456)

One thing that confuses me a little is why I had to add the <c:set... to make param.partNums visible to the pageContext statement?  From the reading I did it seems like I should have been able to use:

String srchPartNums = (String) pageContext.getAttribute("param.partNums", PageContext.REQUEST_SCOPE);

Unless param's aren't in the request scope?  If not what scope are they in?  This is the start of the "root cause" error message:

java.lang.NullPointerException
at java.util.StringTokenizer.(StringTokenizer.java:119)
at java.util.StringTokenizer.(StringTokenizer.java:135)
at org.apache.jsp.pnAdminDescripEdit_jsp._jspService(pnAdminDescripEdit_jsp.java:96)

Thanks,
Eric
0
 

Author Comment

by:EricE
ID: 8141593
Hi,
I just found out how to parse using forTokens with newlines as separators:

<c:forTokens items="${param.partNums}" delims="
" var="partNum" varStatus="status">

You literally have to put a newline between the "'s...

Eric
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
What's worse than having your data encrypted by ransomware? Getting attacked by a so-called "wiper," which simply destroys the data and offers you no hope of ever seeing it again.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question