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

Thread: Please help, how to convert a String to an arithmetic operation?

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

    Question Please help, how to convert a String to an arithmetic operation?

    I have a string, for example "4+3-2" and I want to have the result of these operations, how can I do it?
    The numbers of the string are inserted by the user, so I don't know them unless I run the program, and they come all together from a textfield.
    Could you help me please? Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please help, how to convert a String to an arithmetic operation?

    That sounds like what a compiler does: takes Strings and converts them to operations that a computer can do.
    If the full expression is always the same, 3 numbers with two operators + and -: <number>+<number>-<number> then it gets a lot easier.

    Where are you having problems?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Please help, how to convert a String to an arithmetic operation?

    The first thing you have to learn how to do is splitting the string up into its logical parts.
    If you have the string "111+2222 - 33" you have to split it up to a list / array / collection of strings like these:
    "111"
    "+"
    "2222"
    "-"
    "33"

    Then you can start working from here.
    Try to do this by yourself first, read the documentation for the String class as it provides many useful methods. The Documentation for the Character class might also be useful to you.
    If you are still having problems ask them here in this thread and I am sure somebody will help you out.

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

    Default Re: Please help, how to convert a String to an arithmetic operation?

    thanks for the answers, but maybe I didn't explain well my problem. I don't know which is the string, I ask a user to write the operations in a textfield, so I could get "4+3-2" s well as "5-4-3+44". My question was: does it exist a method that converts every string I get from the textfield to operations and gives me the result?
    However I know how to split a String, and I tried that too but without success..

  5. #5
    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: Please help, how to convert a String to an arithmetic operation?

    does it exist a method that converts every string I get from the textfield to operations and gives me the result?
    No, that's the logic you need to write.
    I tried that too but without success.
    Show what you tried with the results, and we can go from there.

  6. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help, how to convert a String to an arithmetic operation?

    Well, I'm not really practised with java so probably you will find my code naive or worse... just suggest a solution for my problem
    I'm trying to write a calculator program, with JButtons and a JLabel, and I tried to solve the problem by splitting the operation in different numbers as the user presses an operator (+,-,*,/).
    For example:
    if(ref.plus.isFocusOwner())
    			{
    				add=true;
    				String txt=ref.lab.getText();
    				num1=Double.parseDouble(txt);
    				ref.lab.setText(txt+"+");
    			}
    and when the user presses equals:
    if(ref.equals.isFocusOwner())
    			{
    				txt1=String.valueOf(num1);
    				String txt2=ref.lab.getText();
    				String subtxt2=txt2.substring(txt1.length()+1);
    				num2=Double.parseDouble(subtxt2);
    				if(add)
    				{
    				ref.lab.setText(String.valueOf(num1+num2));
    				}
    				if(subtract)
    				{
    					ref.lab.setText(String.valueOf(num1-num2));
    				}
    				if(multiply)
    				{
    					ref.lab.setText(String.valueOf(num1*num2));
    				}
    				if(divide)
    				{
    					ref.lab.setText(String.valueOf(num1/num2));
    				}
     
    				add=false;subtract=false;divide=false;multiply=false;
    			}

    This way it works fine, but when it does the first operation, it gives me problems with the second number.
    If I write "12+12", it does nothing, if I write "12+122" it does just "12+002". The problem could be that when it converts to double it adds to the number the ".0" which increases the "txt1.length()". In the second or further operation, it works, because the number I start with is already a double(Ex:12.0+12=24.0).
    I hope I made myself clear this time, and thanks again for your help (insults for naive code are not welcome)

  7. #7
    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: Please help, how to convert a String to an arithmetic operation?

    We don't insult here - not more than once, anyway. If you believe you've been insulted, report the insulting post using the triangle-exclamation icon at the bottom of the post.

    I didn't trace your code to determine why you're seeing the behavior you're reporting, but I suspect your logic is not correctly finding the next number entered after the operator.

    I recommend parsing the whole string after the user has completed entering the equation. Parse the string into operands and operators, perhaps sequentially from left to right, from that construct the equation, and then perform the necessary calculations to determine the answer. You may not know stacks yet, but using stacks to store the operands and operators is a common equation parsing technique. You can do similarly without stacks by processing each element of the equation as it is parsed.

    You may find other ideas Googling something like 'java equation parser'.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please help, how to convert a String to an arithmetic operation?

    Are the operators only + and -? That will make it easier because they have the same precedence. If other operators with different precedence's (like * and /) are allowed it gets much harder.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Sequences (convert char[] to String) [Replacing String with Character]
    By tpolwort in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:56 PM
  2. Convert List<Set<String>> to string array
    By shatlav in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2013, 08:16 AM
  3. How can I convert a String to Set<String>? Is it possible?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: August 25th, 2010, 09:03 AM
  4. Convert String Literal into Operation
    By SchoolHDD in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 30th, 2010, 03:53 PM
  5. How to convert a String into an Hexadecimal ?
    By lumpy in forum Java Theory & Questions
    Replies: 2
    Last Post: February 16th, 2010, 05:01 PM