Solved

Remove any spacial character and numbers from string in JSP

Posted on 2009-04-12
13
369 Views
Last Modified: 2012-05-06
How I can remove any spacial character and numbers from string in JSP ?

for example
String vr_Statement = "Hello Mr Saloman 123 {0} Ok I will go to the {1} school - at 7 oclock"

I want to remove the spacial charcter and numbers from last string variables can anybody help me ?
0
Comment
Question by:bworld
  • 6
  • 5
  • 2
13 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 24124932
String vr_Statement = "Hello Mr Saloman 123 {0} Ok I will go to the {1} school - at 7 oclock";
char[] newstr = vr_Statement.toCharArray();
StringBuffer nb = newStringBuffer();
for(char a:newstr)
{
if(Character.isLetter(c);
nb.append(c);
}

String final = nb.toString();

0
 

Author Comment

by:bworld
ID: 24124938
Hello Kuldeepchaturvedi

Can you explain to me

for(char a:newstr)
{
if(Character.isLetter(c);
nb.append(c);
}
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 24124958
its just another way of writing the for loop.

same thing could have been written as

for(int i=0; i<newstr.length;i++)
{
if(Character.isLetter(newstr[i]);  //Here I am using a static method of Class Character to check if its a letter
nb.append(newstr[i]);  //here I am simply adding it to the stringbuffer to make a string after the loop finishes.
}
0
 

Author Comment

by:bworld
ID: 24124992
The code attach give me error please check it
<%

String vr_Statement = "Hello Mr Saloman 123 {0} Ok I will go to the {1} school - at 7 oclock";

out.print (vr_Statement);
 

char[] newstr = vr_Statement.toCharArray();

StringBuffer nb = newStringBuffer();
 

for(int i=0; i<newstr.length;i++)

{

	if (Character.isLetter(newstr[i])

	{

		nb.append(newstr[i]);

	}

}
 

String final = nb.toString();
 

out.print ("<p>");

out.print (final);

out.print ("<p>");

%>

Open in new window

0
 

Author Comment

by:bworld
ID: 24125006
Please check the script give me error
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>Untitled Document</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
 

<body>

<%

String vr_Statement = "Hello Mr Saloman 123 {0} Ok I will go to the {1} school - at 7 oclock";

out.print (vr_Statement);
 

char[] newstr = vr_Statement.toCharArray();

StringBuffer nb = newStringBuffer();
 

for(int i=0; i<newstr.length;i++)

{

	if (Character.isLetter(newstr[i]))

	{

		nb.append(newstr[i]);

	}

}
 

String final = nb.toString();
 

out.print ("<p>");

out.print (final);

out.print ("</p>");

%>

</body>

</html>

Open in new window

0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 24125455
can you post the error that you got? It always helps a lot to see the actual error to debug rather than just looking at code and knowing that there may be an error in code..
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:bworld
ID: 24125776
This is error
HTTP Status 500 - 
 

--------------------------------------------------------------------------------
 

type Exception report
 

message 
 

description The server encountered an internal error () that prevented it from fulfilling this request.
 

exception 
 

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

An error occurred at line: 10 in the jsp file: /test5.jsp

Generated servlet error:

Syntax error on token "final", delete this token
 

An error occurred at line: 10 in the jsp file: /test5.jsp

Generated servlet error:

Syntax error on token "final", invalid ArgumentList
 
 

	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)

	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)

	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 
 

root cause 
 

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

An error occurred at line: 10 in the jsp file: /test5.jsp

Generated servlet error:

Syntax error on token "final", delete this token
 

An error occurred at line: 10 in the jsp file: /test5.jsp

Generated servlet error:

Syntax error on token "final", invalid ArgumentList
 
 

	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)

	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)

	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:414)

	org.apache.jasper.compiler.Compiler.compile(Compiler.java:297)

	org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)

	org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)

	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)

	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:303)

	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 
 

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.
 
 

--------------------------------------------------------------------------------
 

Apache Tomcat/5.5.17

Open in new window

0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 50 total points
ID: 24125818
I am just trying to help until kuldeep returns here. Tell us what output you are looking for.
>StringBuffer nb = newStringBuffer();
should be  
StringBuffer nb = new StringBuffer();
>String final = nb.toString();
>out.print (final);
final is a reserved word in Java.
change to something else
String final2 = nb.toString();
...
out.print (final2);
0
 
LVL 27

Expert Comment

by:rrz
ID: 24125820
You have 26 open questions. You should close some of those if you want more people to help you.
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 24128053
thanks for covering me rrz...
bworld try below code.. its basically the two fixes that rrz gave you in earlier post.
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>Untitled Document</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

 

<body>

<%

String vr_Statement = "Hello Mr Saloman 123 {0} Ok I will go to the {1} school - at 7 oclock";

out.print (vr_Statement);

 

char[] newstr = vr_Statement.toCharArray();

StringBuffer nb = new StringBuffer();

 

for(int i=0; i<newstr.length;i++)

{

        if (Character.isLetter(newstr[i]))

        {

                nb.append(newstr[i]);

        }

}

 

String final2 = nb.toString();

 

out.print ("<p>");

out.print (final2);

out.print ("</p>");

%>

</body>

</html>

Open in new window

0
 

Author Comment

by:bworld
ID: 24128117
Ok the code working fine

But I need remove all spacial character only but I need to remain the space between words.
0
 

Author Comment

by:bworld
ID: 24128119
I need the output as follow

Hello Mr Saloman Ok I will go to the school at oclock
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 450 total points
ID: 24128121
If you want to keep spaces in the string use following code.
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>Untitled Document</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

 

<body>

<%

String vr_Statement = "Hello Mr Saloman 123 {0} Ok I will go to the {1} school - at 7 oclock";

out.print (vr_Statement);

 

char[] newstr = vr_Statement.toCharArray();

StringBuffer nb = new StringBuffer();

 

for(int i=0; i<newstr.length;i++)

{

        if (Character.isLetter(newstr[i])||Character.isWhitespace(newstr[i]))

        {

                nb.append(newstr[i]);

        }

}

 

String final2 = nb.toString();

 

out.print ("<p>");

out.print (final2);

out.print ("</p>");

%>

</body>

</html>

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
default methods vs abstract class 1 100
jersey maven project error 7 134
Fisheye tool 2 101
authenticate preemptively soap ui option 3 108
HOW TO: Install and Configure VMware vSphere Hypervisor 6.5 (ESXi 6.5), Step by Step Tutorial with screenshots. From Download, Checking Media, to Completed Installation.
This article will show you how to create an ISO CD-ROM/DVD-ROM image (*.iso), and MD5 checksum signature, for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5). It's a good idea to compare checksums, because many installations fail because of a corr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now