1

Given a string representing a simple arithmetic expression, solve it and return its integer value. The expression consists of two numbers with a + or – operator between the numbers, i.e., it will of the form x+y or x-y where x and y are not negative

MyApproach

I created the NewString and stored the first string till the operator does not come.I took the operator in the character position.I created the second string and stored the rest of the strings into the new String.I then converted them into numbers using parseInt.And then I added the numbers.

What I want to do 123+82=205

I am doing 123+43+82=248.I am not able to figure out how to position the character.

Can anyone guide me what I am doing wrong?

public int solve(String str)
{
  String strnew1="";
  String strnew2="";
  int i=0;
  char ch1=str.charAt(i);
  while((ch1>=48)&&(ch1<=57))
  {
      strnew=strnew+ch1;
      i++;
  }
  int p=str.charAt(i);
  i++;

  while((ch1>=48)&&(ch1<=57))
  {
      strnew2=strnew2+ch1;
      i++;
      if(i==str.length())
      {
          break;
      }


  }
  int n1=Integer.parseInt(strnew1);
  int n2=Integer.parseInt(strnew2);
  n1=n1+p+n2;
  return n1;

}

Test Case result.

  Parameters          Actual Output   ExpectedOutput

  123+82              248             205
Jason arora
  • 550
  • 3
  • 8
  • 22

3 Answers3

1

Here is a nice way to accomplish your task. Basically, you iterate till you find the '+' r '-' sign and meanwhile append characters to a String. Now maintain a boolean value which tells you to add or subtract and set this value when you reach the sign. Now, iterate past the operator sign and append characters to another string. Finally, parse them into integers, add/subtract them and return the result.

 public static int Solve(String input) //Assume input="100+50" for eg.
    {
        int cnt=0;
        boolean op=false; //Default set to subtract
        String raw_a="", raw_b="";
        while(input.charAt(cnt)!='+')
            raw_a+=input.charAt(cnt++); //The first part
        if(input.charAt(cnt)=='+') //setting the operation
            op=true;
        cnt++;
        while(cnt!=input.length())
            raw_b+=input.charAt(cnt++); //the second part
        int a=Integer.parseInt(raw_a), b=Integer.parseInt(raw_b); //parsing them
        int ans =op? a+b: a-b; //If true then add else subtract
        return ans; //Return the ans
    }
gabbar0x
  • 4,046
  • 5
  • 31
  • 51
0

What is the ASCII value of + -> 43.

What is the difference between 248 and 205, 43.

Got the mistake?

You are not actually adding those 2 numbers, you are adding those two numbers with the ASCII value of the operator.


What you should be doing is.

if(p == '+')//check if it is a addition
{
 sum = n1 + n2;
}
else
 sum = n1 - n2;
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
0

You cannot use the operator like this.

Well, you add

  int p=str.charAt(i);

if charAt(i) is a '+' symbol, you add additional 43 ('+' == ascii 43) by implicitly casting a char to int.

Better define 2 cases ('+' / '-') and use the operator:

if (p == 43) { //or p == '+'
    return n1 + n2;
} else if (p == 45) {  //or p == '-'
    return n1 - n2
}
return -1; //undefined
Jörn Buitink
  • 2,906
  • 2
  • 22
  • 33
  • Buitink You nailedit.I was thinking of this but I thought If it can be cast and then I also though I need to check for each operation and define the if else – Jason arora Dec 10 '15 at 07:51