Results 1 to 6 of 6
Thread: String split method
- 12-14-2010, 01:14 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 49
- Rep Power
- 0
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
- 12-14-2010, 01:24 PM #2
Check out the Pattern API to figure out how to use regular expressions.
Pattern (Java Platform SE 6)
- 12-14-2010, 01:52 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 49
- Rep Power
- 0
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
- 12-14-2010, 01:55 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-14-2010, 01:55 PM #5
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
- 12-14-2010, 02:01 PM #6
Member
- Join Date
- Apr 2009
- Posts
- 49
- Rep Power
- 0
Similar Threads
-
Split a String with split()--Help
By danilson in forum New To JavaReplies: 7Last Post: 11-19-2010, 04:08 PM -
String split method
By counterfox in forum New To JavaReplies: 2Last Post: 11-12-2010, 11:06 PM -
Please help me with Split method> String
By javanew in forum New To JavaReplies: 5Last Post: 04-02-2010, 06:15 PM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks