StringTokenizer() with "\" character

Discussion in 'Java' started by snehapshinde@gmail.com, Aug 30, 2008.

  1. Guest

    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!
     
    , Aug 30, 2008
    #1
    1. Advertising

  2. wrote in news:c3336a2c-6f21-4f5a-bf91-748b3f91aa26
    @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);
     
    Donkey Hottie, Aug 30, 2008
    #2
    1. Advertising

  3. Lars Enderin Guest

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

    >
    > 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.
    >
    > <http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.6>
    >> 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 ".
     
    Lars Enderin, Aug 30, 2008
    #3
  4. Roedy Green Guest

    Roedy Green, Sep 1, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Hanna

    Re: StringTokenizer Help

    Phil Hanna, Jul 1, 2003, in forum: Java
    Replies:
    0
    Views:
    889
    Phil Hanna
    Jul 1, 2003
  2. ARMANDO  PADILLA

    StringTokenizer help

    ARMANDO PADILLA, Jul 4, 2003, in forum: Java
    Replies:
    3
    Views:
    625
    Chris Smith
    Jul 4, 2003
  3. Liang Yew
    Replies:
    3
    Views:
    660
    Jacob
    Jul 21, 2003
  4. Jim Crowell

    StringTokenizer Question?

    Jim Crowell, Aug 20, 2003, in forum: Java
    Replies:
    9
    Views:
    3,793
    Roedy Green
    Aug 23, 2003
  5. Newbie
    Replies:
    4
    Views:
    1,480
    Stefan Siegl
    Dec 7, 2003
Loading...

Share This Page