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

Shiva Mohan

Ranch Hand

Posts: 484

posted 10 years ago

- 0

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.

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.

posted 10 years ago

- 0

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

"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

I agree. Here's the link: http://aspose.com/file-tools |