# Simple Calculator

• 01-06-2011, 01:54 AM
marksey07
Simple Calculator
Trying to build a simple calculator

Have this code;

public double result_of(String equation)
{
if (equation.contains("+")){
String[] split_up_equation = String.split([+]);
double result = 0.0;
for (int i=0; i<split_up_equation.length;i++)
{
result += result_of(split_up_equation[i]);
}
return result;
}
}

can anyone tell me why java doesnt like line 4, String.split([+]);

thanks
• 01-06-2011, 01:56 AM
JavaHater
try
Code:

`...split("\\+");`
"+" is special character in regex, so you need to escape.

also, in your original qns, you use double quotes
Code:

`... split("[+]")`
• 01-06-2011, 03:31 PM
marksey07
ok, i have tried split("\\+");

but eclipse doesnt like it, I get yhe message - 'cannot make a static reference to the non-static method split(String) from the type String'

can you explain the problem?

thanks
• 01-06-2011, 03:40 PM
JosAH
Quote:

Originally Posted by JavaHater
try
Code:

`...split("\\+");`
"+" is special character in regex, so you need to escape.

The + character loses its special meaning in ranges [ ... ] so [+] is fine between double quotes "[+]". Know your regular expression syntax.

Code:

```public class T {         public static void main (String[] args) {                 String s= "foo+bar+baz";                 String[] a= s.split("[+]");                 System.out.println(Arrays.toString(a));         } }```
Jos
• 01-06-2011, 04:30 PM
JavaHater
Quote:

Originally Posted by JosAH
The + character loses its special meaning in ranges [ ... ] so [+] is fine between double quotes "[+]". Know your regular expression syntax.

once again you demonstrate your inability to read. Look at my post carefully. I told OP to use double quotes. And stop giving the impression that only you know about regex syntax and nobody else does.
• 01-06-2011, 04:43 PM
JosAH
Quote:

Originally Posted by JavaHater
once again you demonstrate your inability to read. Look at my post carefully. I told OP to use double quotes. And stop giving the impression that only you know about regex syntax and nobody else does.

The OP used the form [+] which is correct except for the quotes. I do know the syntax of regular expressions and many do too but you most certainly aren't one of them.

Jos
• 01-06-2011, 04:47 PM
JavaHater
Quote:

Originally Posted by JosAH
The OP used the form [+] which is correct except for the

yes, that's why i told him to use quotes. Learn to read properly first before you judge anybody else.

Quote:

I do know the syntax of regular expressions and many do too but you most certainly aren't one of them.
nah I doubt that,...you obviously don't know how to read, much less know about regex.
• 01-06-2011, 04:57 PM
KevinWorkman
Alright, I can see trolling unwarranted anger at me (as seen on other forums), but Jos is pretty much unhateable.

I'm going on vacation tomorrow. How much do you want to bet you're banned before I get back?
• 01-06-2011, 05:07 PM
JavaHater
Quote:

Originally Posted by KevinWorkman
but Jos is pretty much unhateable.

ermm, that's a joke right?
Quote:

How much do you want to bet you're banned before I get back?
before you do that, read the entire thread and see who is trolling first.
• 01-06-2011, 05:11 PM
JavaHater
Quote:

Originally Posted by marksey07
ok, i have tried split("\\+");

but eclipse doesnt like it, I get yhe message - 'cannot make a static reference to the non-static method split(String) from the type String'

can you explain the problem?

thanks

read my first post, you will see 2 methods you can use, either "\\+" or "[+]" (double quotes). You will have no problem splitting your strings using these 2 methods.
As for your error message, you probably did not declare static method, OR you did not instantiate an object. You have to post your code to see why ....
• 01-06-2011, 05:28 PM
JosAH
Quote:

Originally Posted by JavaHater
yes, that's why i told him to use quotes. Learn to read properly first before you judge anybody else.

Yes, and you also told the OP to escape the plus sign which is not needed in the form the OP used: [+]. The OP indeed needed to use double quotes. I read your original reply (by email) in which you called me an idiot. I reported you again.

Jos
• 01-06-2011, 07:08 PM
Fubarable
FYI: java-hater to be discussed in moderator's forum. I'm closing this thread for now as it is getting off track.
• 01-09-2011, 07:30 AM
DarrylBurke
Um, the thread isn't closed and the troll is still trolling.

db