Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: tokenizing a string with escape sequence

  1. #1
    Junior Member
    Join Date
    May 2012
    Location
    Singapore
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question tokenizing a string with escape sequence

    hello..

    i'm trying to use string tokenizer on a string contains directory of a particular file..

    for example, StringTokenizer stringTokenizer = new StringTokenizer("C:\Documents\image.jpg", ":\");

    but i got error message from Eclipse : invalid escape sequence

    is there a way to solve this?

    thank you in advance.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: tokenizing a string with escape sequence

    That is because \D and \i are not valid escape sequences.
    The valid ones are:
    • \b
    • \f
    • \n
    • \r
    • \t
    • \"
    • \'


    Also note that since \" is, then ":\" will be seen as a missing quote to end the string

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: tokenizing a string with escape sequence

    Quote Originally Posted by nosxlimit View Post
    ... solve this....
    The backslash is a very special character in Java strings (and in regular expressions, in general).


    If you want a String to have a backslash, and you are initializing with a string literal (something in quotes " "), then inside the quotes, the backslash must be written as two backslashes.

    So: For everything you have between quote marks, replace \ by \\:

            StringTokenizer st = new StringTokenizer("C:\\Documents....etc

    Cheers!

    Z
    Last edited by Zaphod_b; July 24th, 2012 at 04:29 PM.

  4. #4
    Junior Member
    Join Date
    May 2012
    Location
    Singapore
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: tokenizing a string with escape sequence

    Quote Originally Posted by jps View Post
    That is because \D and \i are not valid escape sequences.
    The valid ones are:
    • \b
    • \f
    • \n
    • \r
    • \t
    • \"
    • \'


    Also note that since \" is, then ":\" will be seen as a missing quote to end the string
    Quote Originally Posted by Zaphod_b View Post
    The backslash is a very special character in Java strings (and in regular expressions, in general).


    If you want a String to have a backslash, and you are initializing with a string literal (something in quotes " "), then inside the quotes, the backslash must be written as two backslashes.

    So: For everything you have between quote marks, replace \ by \\:

            StringTokenizer st = new StringTokenizer("C:\\Documents....etc

    Cheers!

    Z
    thanks for the reply for both of you..
    the thing is, i can't just change the backslash into double backslash because i'm obtaining the string by using string = file.getAbsolutePath();..
    so is there another way, maybe?

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: tokenizing a string with escape sequence

    Quote Originally Posted by nosxlimit View Post
    ...another way, maybe...
    My response was an attempt to resolve a specific problem with the example that you posted. I mean, I tried to answer the question you asked. My crystal ball is cloudy today (marine layer down to ground level) and I probably won't have much luck with the question(s) you don't ask.

    Anyhow...


    As I mentioned, the double-backslash thing is for string literals (constant sequences of characters between quotation marks). Once it's in the String, it's in the String. How the String is used is another subject all together.

    Post a complete program that you are trying to use that gives a specific problem.

    Tell us what happens when you compile. (If there are compiler error messages, paste them into the post exactly as they appear. Don't paraphrase.)

    If it compiles OK but doesn't execute properly, then tell us exactly what happened.


    Cheers!

    Z
    Last edited by Zaphod_b; July 24th, 2012 at 05:00 PM.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: tokenizing a string with escape sequence

    Quote Originally Posted by nosxlimit View Post
    ...i can't just change the backslash into double backslash...
    Why not?
    String filePathModified = myFilePathEditingMethod(filePathOriginal);
     
    private String myFilePathEditingMethod(String filePathToBeDoubleSlashed){
    //look for\
    //if \ found, add extra \
    //return editedVersionOfString
    }

    This should give you a push in the right direciton, though I wouldn't try to implement the method as it is drawn up here.

  7. #7
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: tokenizing a string with escape sequence

    Quote Originally Posted by jps View Post
    ...
    //look for\
    //if \ found, add extra \
    //return editedVersionOfString
    }
    Why the heck would you want to put extra stuff in the string? What possible value would that add?

    Why not just something like
            // For test purposes we give it the path.
            // A "real" program at this point would just be given the File object
            // and could use getAbsolutePath() to discover the complete path.
            //
            String fileName = "F:\\home\\Zaphod\\java\\narrative.txt";
            File infile = new File(fileName);
            String str = infile.getAbsolutePath();
            System.out.printf("str: %s\n", str);
            StringTokenizer st = new StringTokenizer(str, ":\\");
            while(st.hasMoreTokens())
            {
                System.out.printf("Token: %s\n", st.nextToken());
            }

    Output

    str: F:\home\Zaphod\java\narrative.txt
    Token: F
    Token: home
    Token: Zaphod
    Token: java
    Token: narrative.txt
    Last edited by Zaphod_b; July 24th, 2012 at 05:24 PM.

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: tokenizing a string with escape sequence

    Quote Originally Posted by Zaphod_b View Post
    Why the heck would you want to...
    Quote Originally Posted by jps View Post
    ...
    This should give you a push in the right direciton, though I wouldn't try to implement the method as it is drawn up here.
    Because I don't intend to spoonfeed answers, but give something to think about.

  9. #9
    Junior Member
    Join Date
    May 2012
    Location
    Singapore
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: tokenizing a string with escape sequence

    Quote Originally Posted by jps View Post
    Why not?
    String filePathModified = myFilePathEditingMethod(filePathOriginal);
     
    private String myFilePathEditingMethod(String filePathToBeDoubleSlashed){
    //look for\
    //if \ found, add extra \
    //return editedVersionOfString
    }

    This should give you a push in the right direciton, though I wouldn't try to implement the method as it is drawn up here.
    i tried to do what you're suggesting, but intead of adding extra '\', i think it's better to replace it with something else (easier to be tokenized later on)..
    so i used replace.. but still, it give me error because of the single backslash..

    Quote Originally Posted by Zaphod_b View Post
    Why the heck would you want to put extra stuff in the string? What possible value would that add?

    Why not just something like
            // For test purposes we give it the path.
            // A "real" program at this point would just be given the File object
            // and could use getAbsolutePath() to discover the complete path.
            //
            String fileName = "F:\\home\\Zaphod\\java\\narrative.txt";
            File infile = new File(fileName);
            String str = infile.getAbsolutePath();
            System.out.printf("str: %s\n", str);
            StringTokenizer st = new StringTokenizer(str, ":\\");
            while(st.hasMoreTokens())
            {
                System.out.printf("Token: %s\n", st.nextToken());
            }

    Output

    str: F:\home\Zaphod\java\narrative.txt
    Token: F
    Token: home
    Token: Zaphod
    Token: java
    Token: narrative.txt
    How if the obtained string is like below (single backslash)?
            String fileName = "F:\home\Zaphod\java\narrative.txt";
    Last edited by nosxlimit; July 24th, 2012 at 05:52 PM.

  10. #10
    Junior Member
    Join Date
    May 2012
    Location
    Singapore
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: tokenizing a string with escape sequence

    thanks Zaphod_b and jps..
    thanks for replying..
    i think i will just use the simple one, which is save the image temporarily, use it, then delete it again.. hehe..
    thank you..

Similar Threads

  1. Sequence Classification
    By SilentNite17 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 1st, 2012, 08:25 AM
  2. Question to Pattern.matches and escape sequences
    By steppenwolf in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 04:29 PM
  3. Sequence
    By r_james14 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2012, 10:05 PM
  4. BFS Travel not in the sequence
    By keat84 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 08:19 PM
  5. Using \n escape sequence in a toString method
    By coolidge in forum Java Theory & Questions
    Replies: 4
    Last Post: September 22nd, 2011, 04:14 PM