So here is something i am trying to do:
1.Store a mathematical expression eg. 23*32+12 in the String datatype.
2.Now i have to calculate the value of that expression.
So i thought of converting the string to integer using parseInt , but for this the string need not contain *,+ and other operators.So how can i calculate the value of that expression?

Recommended Answers

All 4 Replies

with the indexOf method, you'll be able to detect where there are mathematical symbols, such as * + /
next, use the substring method, to divide the String you have, and run the Integer.parseInt method on the substrings you've found.
but make sure you only have non-decimal numbers, otherwise, an int won't give you the correct result.

with the indexOf method, you'll be able to detect where there are mathematical symbols, such as * + /
next, use the substring method, to divide the String you have, and run the Integer.parseInt method on the substrings you've found.
but make sure you only have non-decimal numbers, otherwise, an int won't give you the correct result.

so here is the code i wrote:

class indextut
{
	public static void main(String args[])
	{
		String et="221+33*123+3";
		String co = null;
		int count1=0,i=0,j=0,k=0,res=0;
		int count2=0,no=0;
		int converted ;
		int[] rray= new int[20];
		char[] st=new char[20];
		for(i=0;i<et.length();i++)
		{
			if(!Character.isDigit(et.charAt(i)))
			{
				no++;
			}
		}
		for(i=0;i<et.length();i++)
		{
			if(!Character.isDigit(et.charAt(i)))
			   {
				count2++;
				co=et.substring(count1,i);
				converted = Integer.parseInt(co);
		        rray[j]=converted; 
				st[k]=et.charAt(i);
		        count1=i+1;
			    j++;
			    k++;
			   }
			if((count2) == no)
			   {
				co=et.substring(count1,et.length());
				converted = Integer.parseInt(co);
		        rray[j]=converted; 
				break;
			   }
					
		}
	
		for(i=0;i<k;i++)
			{
			 if(i==0)
			 {
				 if(st[i]=='+') 
				    res=rray[i]+rray[i+1];
			     if(st[i]=='-') 
				    res=rray[i]-rray[i+1];
			     if(st[i]=='/') 
			     	  res=rray[i]/rray[i+1];
		    	 if(st[i]=='%') 
			  	  res=rray[i]%rray[i+1];
			   if(st[i]=='*') 
				  res=rray[i]*rray[i+1];
			 }
			 else
			 {
				 if(st[i]=='+') 
				   res=res+rray[i+1];
				 if(st[i]=='-') 
				   res=res-rray[i+1];
				 if(st[i]=='/') 
					res=res/rray[i+1];
			     if(st[i]=='%') 
				 	res=res%rray[i+1];
				 if(st[i]=='*') 
					res=res*rray[i+1];
				 
			 }
			 			 
			}
		System.out.print("res="+res);
	}
	
}

using this i am getting the result but now i am having one problem:
The result is calculated in linear fashion instead i want it to follow BODMAS rule while
computing the value.
For eg my string is=221+33*123+3
so the code does this:
221+33=254
254*123=31242
31242+3=31245
So the result is 31245
now if i have to follow bodmas it should be:
33*123=4059
and then it should add 221 and 3,so the result would be 4283.
So how should i achieve this?

Normally you would have a stack where you can push values and operators as you parse them, and pop them when it's time time to execute each operator.

Normally you would have a stack where you can push values and operators as you parse them, and pop them when it's time time to execute each operator.

Ok thanks i will try to do it that way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.