Results 1 to 6 of 6
  1. #1
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default String split method

    Hi guys,

    I'm trying to write a method to convert a mathematical expression into a function using Java

    I'm starting with really basic functions, i.e.

    x^2 + x + 2

    now as a starting point, I'm hoping to split the String i.e.

    "x^2 + x + 2"

    using the split method of the String class, now given I was to split using the addition symbols I thought I would use

    "x^2 + x + 2".split("+");

    however this produces the following error (using Netbeans)

    Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
    +
    ^
    at java.util.regex.Pattern.error(Pattern.java:1713)
    at java.util.regex.Pattern.sequence(Pattern.java:1878 )
    at java.util.regex.Pattern.expr(Pattern.java:1752)
    at java.util.regex.Pattern.compile(Pattern.java:1460)
    at java.util.regex.Pattern.<init>(Pattern.java:1133)
    at java.util.regex.Pattern.compile(Pattern.java:823)
    at java.lang.String.split(String.java:2292)
    at java.lang.String.split(String.java:2334)
    at functionbuilder.Main.main(Main.java:19)
    Java Result: 1


    Was just wondering if this can be overcome with the split method?

    Thanks in Advance,

    Cheers,

    David

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default

    Check out the Pattern API to figure out how to use regular expressions.

    Pattern (Java Platform SE 6)

  3. #3
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default

    Hi Kevin,

    Thanks for linking that, Sorry I'm fairly new to Java and am still puzzled by this :-(
    When I try
    Pattern p = Pattern.compile("+")
    I get a similar error as before, I'm sure I'm missing something simple - any further suggestions would be greatly appreciated.

    Cheers,

    David

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

    Default

    Quote Originally Posted by DavidG24 View Post
    When I try
    Pattern p = Pattern.compile("+")
    I get a similar error as before, I'm sure I'm missing something simple - any further suggestions would be greatly appreciated.
    The '+' character has a special meaning in regular expressions: it's the transitive closure of a regular expression: 'one or more times'. You have to escape it to make it an ordinary character: "\\+".

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default

    Uh, yeah. That error would be expected. What does the API say about using a "+" symbol? What does it say about escaping literals?

    Edit- Jos made it easy for you. :p

  6. #6
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default

    Hi JosAH,

    cheers for that mate! working now :-)
    And thanks again Kevin..

    David

Similar Threads

  1. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 05:08 PM
  2. String split method
    By counterfox in forum New To Java
    Replies: 2
    Last Post: 11-13-2010, 12:06 AM
  3. Please help me with Split method> String
    By javanew in forum New To Java
    Replies: 5
    Last Post: 04-02-2010, 07:15 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 09:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:32 PM

Posting Permissions

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