[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

character replacement issue

Posted on 2005-04-13
13
Medium Priority
?
224 Views
Last Modified: 2010-04-01
Im new to JSP. PHP is my primary language.

What i want to do i a little replacement of some characters within a string.

In php this is easy done:
<?
$str1 = "1,0 2,0 3,0 4,0 5,0";
$str2 = ereg_replace(",0 "," \"</OPTION>\n<OPTION>",$str1);

echo "<SELECT>\n<OPTION>";
echo $str2
echo "</OPTION>\n</SELECT>";
?>

But when I'm about to do the same in JSP, it wont work. I'm using String.replace() class.

My JSP-script looks like this:

<%
String strA = "1,0 2,0 3,0 4,0 5,0";
String strB = strA.replace(",0 ", " \"</OPTION>\n<OPTION>");

out.println("<SELECT>\n<OPTION>");
out.println(strB);
out.println("</OPTION>\n</SELECT>");
%>

Im using Apache Tomcat/4.0.1, and I get the following error-report:

> org.apache.jasper.JasperException: Unable to compile class for JSP

> Generated servlet error:
> /var/tomcat4/work/bythjul.com/jsp/test$jsp.java:97: Incompatible type for method. Can't convert java.lang.String to char.
>                 String strB = strA.replace(",0 ", " \"</OPTION>\n<OPTION>");
>                                                      ^

> Generated servlet error:
> /var/tomcat4/work/bythjul.com/jsp/test$jsp.java:97: Incompatible type for method. Can't convert java.lang.String to char.
>                 String strB = strA.replace(",0 ", " \"</OPTION>\n<OPTION>");
>                                                              ^
> 2 errors


Thanks' in advance,
Klodorff
0
Comment
Question by:Klodorff
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13772482
replace() takes a charSequence, you probably need to use replaceAll() or replaceFirst() depending on your requirements..

Just use it as you were trying to in the above example
0
 
LVL 8

Expert Comment

by:nagki
ID: 13772507
In Java, using replace u can replace only single characters with other single characters..

use replaceAll:

String strA="1,0 2,0 3,0 4,0 5,0";
strA.replaceAll(",0","</OPTION>\n<OPTION>");
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13772522
e.g.
String strB = strA.replaceAll(",0 ", " \"</OPTION>\n<OPTION>");
0
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!

 
LVL 1

Author Comment

by:Klodorff
ID: 13772939
This is what I get if I use replaceAll/replaceFirst:

>/var/tomcat4/work/bythjul.com/jsp/test$jsp.java:97: Method replaceAll(java.lang.String, java.lang.String) not found in  >class java.lang.String.
>               String strB = strA.replaceAll(",0 ", " \"</OPTION>\n<OPTION>");
>                                                      ^
0
 
LVL 19

Assisted Solution

by:Kuldeepchaturvedi
Kuldeepchaturvedi earned 600 total points
ID: 13773119
what version of jdk you are using?
replaceAll and replaceFirst() were introduced in jdk1.4... that why I normally suggest to use the string tokenizer for this purpose...

i.e.
<%
StringTokenizer strA = new StringTokenizer("1,0 2,0 3,0 4,0 5,0",",");
out.println("<SELECT>\n<OPTION>");
while(strA.hasMoreTokens())
{
String temp=strA.nextToken();
if(temp.equals("0"))
out.println("</OPTION>\n<OPTION>");
else
out.println(temp);
}
out.println("</OPTION>\n</SELECT>");
%>


Easier way though is to upgrade to jdk1.4
0
 
LVL 1

Author Comment

by:Klodorff
ID: 13773193
since I don't run the hosting myself, it's hard to update jdk.

Your StringTokenizer solution returns this error:
/var/tomcat4/work/bythjul.com/jsp/test$jsp.java:96: Class org.apache.jsp.stringTokenizer not found.
                stringTokenizer strA = new stringTokenizer("1,0 2,0 3,0 4,0 5,0",",");
                ^
0
 
LVL 1

Author Comment

by:Klodorff
ID: 13773204
I've tried also with capital S in StringTokenizer.
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13773247
you will have to import the package for it.. at the top of the jsp put

<%@ page import="java.util.* "%>

and yes it is StringTokenizer, sorry for the typo...
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13773267
Don't forget you need to import the java.util.StringTokenizer class for Kully's method. (using * means import all in that package) at the top of your jsp
e.g.
<%@ page import="java.util.*%>
0
 
LVL 1

Author Comment

by:Klodorff
ID: 13773613
Getting better, no error-page left. But the result is not what it should be.
Right now, this is the HTML-output of StringTokenizer snippet:

<SELECT>
<OPTION>
1
0 2
0 3
0 4
0 5
</OPTION>
<OPTION>
</OPTION>
</SELECT>

and I want it like this:

<SELECT>
<OPTION>1 "</OPTION>
<OPTION>2 "</OPTION>
<OPTION>3 "</OPTION>
<OPTION>4 "</OPTION>
<OPTION>5 "</OPTION>
</SELECT>
0
 
LVL 29

Accepted Solution

by:
bloodredsun earned 400 total points
ID: 13773909
This:
            StringTokenizer strA = new StringTokenizer("1,0 2,0 3,0 4,0 5,0"," ,0");
            out.println("<SELECT>");
            while(strA.hasMoreTokens()){
                  out.println("<OPTION>" + strA.nextToken() + "<OPTION>\n");
            }
            out.println("</SELECT>");

outputs this:

<SELECT>
<OPTION>1<OPTION>

<OPTION>2<OPTION>

<OPTION>3<OPTION>

<OPTION>4<OPTION>

<OPTION>5<OPTION>

</SELECT>

Is that what you wanted?
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13773956
But to get exactly what you asked for, use this:

            StringTokenizer strA = new StringTokenizer("1,0 2,0 3,0 4,0 5,0"," ,0");
            out.println("<SELECT>");
            while(strA.hasMoreTokens()){
                  out.println("<OPTION>" + strA.nextToken() + " \"<OPTION>");
            }
            out.println("</SELECT>");

which outputs

<SELECT>
<OPTION>1 "</OPTION>
<OPTION>2 "</OPTION>
<OPTION>3 "</OPTION>
<OPTION>4 "</OPTION>
<OPTION>5 "</OPTION>
</SELECT>

..but you have those hanging quotes which you must be careful of...
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13774027
yeah I missed that there was no commas between zeros...
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

830 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