File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Convert String to operator(+*/-) ,please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert String to operator(+*/-) ,please help" Watch "Convert String to operator(+*/-) ,please help" New topic

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

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 481

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.

StringTokenizer st = new StringTokenizer(str,":");
int noOfColumns = st.countTokens();
dbcolumn = new String[noOfColumns];
while (st.hasMoreElements()) {
dbcolumn [i++] = st.nextToken();
int s=0;
System.out.println(dbcolumn[i]) ;

s=Integer.parseInt( dbcolumn[i]);
dbcolumn[i++].charAt(1) ;
int s1=Integer.parseInt(dbcolumn[i+2]);


Please help me to find out.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


"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

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Convert String to operator(+*/-) ,please help
It's not a secret anymore!