Meaningless Drivel is fun!*
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert String to operator(+*/-) ,please help" Watch "Convert String to operator(+*/-) ,please help" New topic
Author

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

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479
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.


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


}
while(i>dbcolumn.length){
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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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



[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert String to operator(+*/-) ,please help