Results 1 to 4 of 4
  1. #1
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Question Scanner delimiter issue

    I am working with scanner delimiter.

    Here is my code:


    Scanner sc = new Scanner("hello$to$you");
    sc.useDelimiter("$");

    while(sc.hasNext()) System.out.println(sc.next());

    It should print:

    hello
    to
    you


    It prints:
    hello$to$you


    I tried other delimiters such as a letter, and it works FINE. However, when I use the "$" delimiter it appears not to recognize it inside the string. Why is this so? I even tried other symbols and it works fine. However, there is an issue with the "$" delimiter for some reason.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Have a read of the useDelimiter() API documentation. The argument - "$" - is a pattern, not a string to use as the delimiter. Sometimes the two are the same but sometimes they are not because the pattern interpretes some symbols specially.

    The pattern itself is described in the Pattern API docs. If you read that latter link you'll see that $ is a special character meaning "the end of a line". It's handy to have a scanner that delimits things based on end-of-line, but that's not what you want.

    From the "Backslashes, escapes, and quoting" we read:
    The backslash character ('\') serves to introduce escaped constructs, as defined in the table above, as well as to quote characters that otherwise would be interpreted as unescaped constructs. Thus the expression \\ matches a single backslash and \{ matches a left brace.

    So try

    Java Code:
    sc.useDelimiter("\\$");

    You might think the string pattern should be "\$", but remember that because of how Java string literals work "\\$" is a string that is two characters long: a backslash followed by a dollar sign.

    These patterns are called regular expressions. (a term which is useful for googling)
    Last edited by pbrockway2; 10-05-2010 at 09:20 AM.

  3. #3
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default

    Wow perfect explanation. Completely solved and clarified my problem.

    I learned about patterns which I didn't know before so that aspect is completely clarified. Also, your solution saved me a lot of time as well, since I'm not sure I would have been able to come up with the double backslash on my own.

    And thanks for taking the time to pull of all that info up for me, and the links. I appreciate it.

    Also your explanations were totally clear and very informative. Definitely A+.

    Cheers.
    Last edited by PrinceSendai; 10-05-2010 at 10:34 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. How to use delimiter in java
    By adityasirohi in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 11:01 PM
  2. Replies: 1
    Last Post: 02-20-2009, 03:06 PM
  3. Use of Scanner class and Delimiter
    By tjhodge in forum New To Java
    Replies: 3
    Last Post: 02-12-2009, 06:26 PM
  4. delimiter
    By satin in forum New To Java
    Replies: 2
    Last Post: 11-17-2008, 11:50 PM
  5. Class Scanner looping issue
    By Stev0 in forum New To Java
    Replies: 1
    Last Post: 05-25-2008, 07:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •