StringTokenizer() with "\" character

S

snehapshinde

String filename="C:\Program Files\Apache Software Foundation\Tomcat
5.5\webapps\upload\multipartform.htm";

StringTokenizer st=new StringTokenizer(filename,"\",false);
String v="";
while(st.hasMoreTokens())
{
v=st.nextToken();
}
out.print(" filename is "+v+" this");

This piece of code is throwing following exception-
An error occurred at line: 3 in the jsp file: /Save.jsp
Generated servlet error:
String literal is not properly closed by a double-quote

But if i replace "\" with ".", then it is working fine,
What is the problem with "\"?
Plz Help!
 
D

Donkey Hottie

(e-mail address removed) wrote in @k36g2000pri.googlegroups.com:
String filename="C:\Program Files\Apache Software Foundation\Tomcat
5.5\webapps\upload\multipartform.htm";

StringTokenizer st=new StringTokenizer(filename,"\",false);
String v="";
while(st.hasMoreTokens())
{
v=st.nextToken();
}
out.print(" filename is "+v+" this");

This piece of code is throwing following exception-
An error occurred at line: 3 in the jsp file: /Save.jsp
Generated servlet error:
String literal is not properly closed by a double-quote

But if i replace "\" with ".", then it is working fine,
What is the problem with "\"?
Plz Help!

\ is a special character in C and related languages, there are constructs
like

\b (bell)
\a (bell)
\n new line (LF)
\r carriage return (CR)

The character following a \ is assumed to have some special meaning, and it
will not be processed as it is. Java finds something special for \" and
while there will not be anything special that probably is blank.

But putting \\ in there will lead to a special value: \

StringTokenizer st=new StringTokenizer(filename,"\\",false);
 
L

Lars Enderin

Lew said:
You need to double the backslashes.
StringTokenizer st=new StringTokenizer(filename,"\",false);
String v="";
while(st.hasMoreTokens())
{
v=st.nextToken();
}
out.print(" filename is "+v+" this");

This piece of code is throwing following exception-
An error occurred at line: 3 in the jsp file: /Save.jsp
Generated servlet error:
String literal is not properly closed by a double-quote

But if i replace "\" with ".", then it is working fine,
What is the problem with "\"?
Plz [sic] Help!

The problem was programmer error.

It is a compile-time error if the character following a backslash in
an escape is not an ASCII b, t, n, f, r, ", ', \, 0, 1, 2, 3, 4, 5, 6,
or 7.
Which includes ". The error was a missing quote, as stated. Inside a
quoted string, the quoting character must be escaped. Thus "\"" is a
string consisting of one ".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top