Results 1 to 3 of 3
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default Frustrating simple problem.

    Hi!
    Heres the code:
    PHP Code:
    public class PopupSample{
    public static void main(String args[]){
    	String s = "int xcomp.XComponent.width";
    	System.out.println(s.split(".").length);
    }
    }
    As you can see, the string have 2 dots in it.
    It will claim to be NO dots at all when you run this.

    What is the problem?! Dot is not a special character that needs some sort of @@$@$\\@ . to be interpreted as a dot!
    So why wont this simple split action work?

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Frustrating simple problem.

    you need to escape the period.

    Java Code:
    System.out.println(s.split("\\.").length);

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: Frustrating simple problem.

    Quote Originally Posted by Addez View Post
    What is the problem?! Dot is not a special character that needs some sort of @@$@$\\@ . to be interpreted as a dot!
    So why wont this simple split action work?
    A dot is special for the regular expression engine use by the split( ... ) method; a dot means 'any character' and you don't want that. The regular expression syntax wants you to escape the special meaning of that dot (meta) character so you should use "\\." instead of "." the double backslash is needed to keep Javac happy.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Simple but frustrating error
    By JoeHill in forum New To Java
    Replies: 10
    Last Post: 06-02-2011, 09:28 AM
  2. very frustrating.. recursive
    By Yakg in forum New To Java
    Replies: 5
    Last Post: 01-06-2011, 10:25 PM
  3. get index from an array (very frustrating)
    By Yakg in forum New To Java
    Replies: 4
    Last Post: 12-06-2010, 06:26 PM
  4. Please help setting up Tomcat. Very frustrating.
    By wombatvvv in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 09-12-2010, 05:56 AM

Posting Permissions

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