• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert String to operator(+*/-) ,please help

 
Ranch Hand
Posts: 486
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am new to programming.

I am trying to do the foolowing program.

I am using Stack class to calculate simple arithmetic expressions involving integers,
such as 1+2*3.your program would execute operations in the order given,without regarding to the precedence of operators.
*Thus, the expression 1+2*3 should be calculated (1+2)*3=9,not 1+(2*3)=7.

If i get the input as 1+2*3,i know how to convert the string 1,2,3 to Integer.but i don't know how to covert +,* from string type to operator.



Please help me to find out.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

"Operators" aren't a Java data type; there's no way to convert a String to an operator. What you would do instead is write code that does a certain operation based on the contents of a String; i.e., one simple way might something like

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Honestly, I was looking for an answer too. I wanted to make a calculator app so I was trying to turn a string directly into an operation but after looking on the internet for a few hours I found nothing so I ended up making the program myself and since I'm a beginner it wasn't easy. So here s my program it doesn't work with parenthesis or commas. You can only use the four operators "+ - * /"
but besides that, everything seems to be working.

It s pretty simple just start with the division by taking both numbers and the string that represent the operand then cut it into three string then do the operation with both numbers after that replace the operation with the result until there s no division left . After that do the same for multiplication , subtraction, and finally addition

here s a description of my functions


getOp   : get the string of the operation so if you gave it "2+4 * 6/3" you'll get 6/3.
cutString: cut the string into three parts so if you gave it 6/3 you'll get
a string array with arr = ["6","/","3"] inside
Op: takes the the array of cutString and depending on the arr[1] so in this
case "/" it does the math with arr[0] and arr [2] so 6/3 = 2
orderOp : will call the other function and get the result for cutString and replace it in the original equation so "2+4 * 6/3" will turn into "2+4 * 2"
So if you want to call the entire progam on your string do:
String s = "2+4 * 6/3";
orderOp(s);
<br>If I made any grammatical mistakes I'm sorry English is not my first language

   
 
Greenhorn
Posts: 2
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To execute operations in the order given, without regarding to the precedence of operators, we can use parentheses to group the operations in the desired order. In Java, parentheses have the highest precedence, so any expression inside parentheses is evaluated first.

Here's the Java program that takes an arithmetic expression as input and evaluates it in the order given, without regarding to the precedence of operators:



This program first prompts the user to enter an arithmetic expression. It then splits the expression into operands and operators using a regular expression. It then evaluates the expression in the order given, without regarding to the precedence of operators, by iterating over the tokens and applying the operators to the operands. Finally, it prints the result of the expression.

Hope it helpes you :)
 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SR, welcome to the Ranch
 
Sriansh Raj
Greenhorn
Posts: 2
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:SR, welcome to the Ranch



Hehe    >.<     thanks

Just loving it <3
 
reply
    Bookmark Topic Watch Topic
  • New Topic