Java Questiong - Relating to JSP

Status
Not open for further replies.

jagga

Wise, Aged Ars Veteran
122
Hi all
Could you please help me with a small problem that I am having? I am trying to use JSP to write a online guestbook for my site. I am using 'request.getParameter()' to get the user input and then store it in a DB. The problem is that I am using a 'textarea' for 'Message' Portion of the entry. When i do this

String msg = request.getParameter("msg");

The string 'msg' stores the message but without the 'newlines'. It all appears as one big line. Can anyone guide me as to how I can fix this problem?

Muchos gracias for your time!

Thanks
Jagga
 

jagga

Wise, Aged Ars Veteran
122
Wow.. that was the problem. Now I have one more problem :(

So I am trying to replace the '\n' with a '
'. I thought I could use 'replaceAll'. but that is not working. I have a string called 'msg'. I am trying to use it like this

msg.replaceAll("\n", "
");

but it gives me this error:

/usr/local/jakarta-tomcat-4.0.1/work/localhost/_/GuestBook/addMsg$jsp.java:73: Method replaceAll(java.lang.String, java.lang.String) not found in class java.lang.String.


Please advice once more :(

Thanks!
 

Sans

Wise, Aged Ars Veteran
144
You're getting the error because there is no replaceAll() method off of String. There's a replace() but that won't work since it only uses single characters as args. You'll probably have to do something to the effect (code probably not perfect):
Code:
StringBuffer sb = new StringBuffer();

for(int i=0; i < msg.length(); i++)
{
  char c = msg.charAt(i);
  if (c == '\n')
  {
    sb.append("
");
  }
  else
  {
    sb.append(c);
  }
}

There may be a replaceAll type method somewhere but I haven't found it yet.

Have fun.

.Sans
 

KarlShea

Ars Tribunus Militum
2,277
Subscriptor++
If you are using JDK 1.3.x or below, there is no replaceAll. However, if you are using 1.4, there is.

You need to download the JDK 1.4 and you will be able to use it.

replaceAll()

ok I can't get the link to work right.

it's here, you'll have to copy and paste:

http://java.sun.com/j2se/1.4/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)
 

jagga

Wise, Aged Ars Veteran
122
I am using Mac OSX and this is the java version.

java -version
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-root-020219-20:07)
Java HotSpot(TM) Client VM (build 1.3.1, mixed mode

But I ended up writing my own code to fix the problem. Felt kinda bad that I couldn't use something built in; but dont feel that bad now ;). Thanks all for your help.

Laters!
 
Status
Not open for further replies.