Results 1 to 13 of 13
  1. #1
    marksey07 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default 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

  2. #2
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    try
    Java Code:
    ...split("\\+");
    "+" is special character in regex, so you need to escape.

    also, in your original qns, you use double quotes
    Java Code:
    ... split("[+]")
    Last edited by JavaHater; 01-06-2011 at 02:01 AM.

  3. #3
    marksey07 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    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

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

    Default

    Quote Originally Posted by JavaHater View Post
    try
    Java 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.

    Java 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    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.
    Last edited by JavaHater; 01-06-2011 at 04:35 PM.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,775
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JavaHater View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    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.

    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.

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

    Default

    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?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by KevinWorkman View Post
    but Jos is pretty much unhateable.
    ermm, that's a joke right?
    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.

  10. #10
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by marksey07 View Post
    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 ....

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,775
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JavaHater View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    FYI: java-hater to be discussed in moderator's forum. I'm closing this thread for now as it is getting off track.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Um, the thread isn't closed and the troll is still trolling.

    db

Similar Threads

  1. creating a simple calculator
    By hobo in forum New To Java
    Replies: 4
    Last Post: 11-09-2009, 04:09 AM
  2. Simple Calculator Display Problem :(
    By jimbob in forum Java Applets
    Replies: 4
    Last Post: 07-18-2009, 05:13 AM
  3. [SOLVED] Simple Conversion Calculator
    By dbashby in forum New To Java
    Replies: 6
    Last Post: 03-20-2009, 02:06 AM
  4. [SOLVED] Simple Calculator Applet Weird Error
    By sari in forum New To Java
    Replies: 5
    Last Post: 01-28-2009, 05:57 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
  •