Parsing a textarea

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
EricEAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
<%
  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
 
objectsCommented:
<%
   java.util.StringTokenizer st = new java.util.StringTokenizer(text, "\n");
   String a = st.nextToken();
   String b = st.nextToken();
%>
0
 
objectsCommented:
oops, probably want to also add \r in the delimiter string.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
rrzCommented:
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
 
EricEAuthor Commented:
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
 
EricEAuthor Commented:
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
 
EricEAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.