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 5 of 5

Thread: Splitting string in Java error

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Splitting string in Java error

    Hello I am currently trying to split the string "EAM\test" between the \ part. I have gotten the code to work if the \ was a -. But I can't see why the error is occuring

    I have tried

    String test = "EAM-testing";
    String[] parts = test.split("-");
    System.out.println("parts[0] = " + parts[0]);
    System.out.println("parts[1] = " + parts[1]);
    String test1 = "EAM\testing";
    String[] parts1= test1.split("\\");
    System.out.println("parts1[0] = " + parts1[0]);
    System.out.println("parts1[1] = " + parts1[1]);

    The output I see is:
    parts[0] = EAM
    parts[1] = testing
    java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
    \
    ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.split(Unknown Source)
    at java.lang.String.split(Unknown Source)

    The error occurs at line: String[] parts1= test1.split("\\");


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Splitting string in Java error

    Because the split method works with regular expressions. The String "\" is not a valid regular expression because the "\" is the escape character for regular expressions in java.
    Try to use "\\" instead.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Splitting string in Java error

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

  4. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Splitting string in Java error

    Hai Scoobs ,
    In the code, String test1 contains "\" expression. but in the split function of string parts1 contains "//" expression.
    So change it.. it ll work..

    Nijan
    Last edited by copeg; July 28th, 2014 at 01:07 PM. Reason: Removed prmotional links

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Location
    Canada
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: Splitting string in Java error

    The issue is also that the \t in your test1 String object is becoming a tab. As Cornix said, .split() uses a regular expression as a parameter, so it expects something after the escaped backslash. In this case, using "\\s" should work (in regex, \s matches whitespace characters), as it will split the tab created in your String and give you the following output:
    parts1[0] = EAM
    parts1[1] = esting

Similar Threads

  1. Splitting a String which is Double Hours Method
    By midani in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 13th, 2014, 09:35 AM
  2. Splitting a String by number of characters
    By torrie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 24th, 2013, 10:58 PM
  3. Help with splitting string in PDF Generator Function
    By efluvio in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 21st, 2012, 05:43 PM
  4. Splitting String
    By JuLiAnc in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 17th, 2011, 10:23 AM
  5. [SOLVED] Splitting String by Multiple Delimiters
    By relixus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2011, 08:54 PM