Reputation: 163
If I have the equation 7 + 13 = 20
as a string, how would I go about getting the operator symbol + out of the equation?
String operator = fileContent.substring(fileContent.indexOf('+'));
I tried something along these lines, as well as changing several things in it, but I have not been able to get the operator out. I've only been able to get it to return things like + 13 = 20
or 13 = 20
but never the operator alone.
I cannot use regex, try/catch, arrays, or SystemTokenizer for this.
Also, there will not always be spaces between the numbers, and they will not always be just one or two digits in size.
One last thing. I also need it so that if the operator were to be put into something like problem = (firstNumber + operator + secondNumber)
it would actually work to compute the math.
The expected output, or at least what I'm trying to do, is to have it take the first and second number, and read the operator so that it can do the math of the problem. Its really confusing and I'm sorry.
int first = Integer.parseInt(fileContent.substring(0, fileContent.indexOf(" ")));
int second = secondNumber(result, fileContent);
int last = Integer.parseInt(result.substring(result.indexOf("=") + 1));
Here's the code I have to get the first, second, and the answer numbers, but I need to get the operator symbol out in a way that I can put it between the first and second numbers so that it will actually do the math, and so I can check it against the answer number.
Its for an assignment that will act as a "math problem grader" but none of the operators are the same.
I need it to take 7 and 13
and print 20
as the answer.
Upvotes: 2
Views: 2565
Reputation: 13483
This answer will give the output +
. Considering your variable name is operator
, I assume that's what you want.
Using substring
:
String operator = fileContent.substring(fileContent.indexOf('+'), fileContent.indexOf('+') + 1);
or written more readably:
int index = fileContent.indexOf('+');
String operator = fileContent.substring(index, index + 1);
Using charAt
:
char operator = fileContent.charAt(fileContent.indexOf('+'));
or written more readably:
int index = fileContent.indexOf('+');
char operator = fileContent.charAt(index);
Edit:
Here is how to find the first and second numbers:
String eqn = "7 + 13 = 20";
int operatorIndex = eqn.indexOf(operator); // already defined operator as a String
int equalIndex = eqn.indexOf('=');
int first = Integer.parseInt(eqn.substring(0, operatorIndex).trim());
int second = Integer.parseInt(eqn.substring(operatorIndex + 1, equalIndex).trim());
Now we have String operator = "+";
and int first = 7;
and int second = 13;
Click this sentence if you don't know what a switch statement is in Java. It's just a different way of using an if-structure. It's not difficult to understand. I can rewrite is using an if-structure if you need me too, though.
I would just use this:
String operator = "+";
int first = 7; int second = 13;
switch (operator)
{
case "+":
System.out.print("" + (first + second));
break;
case "-":
System.out.print("" + (first - second));
break;
case "*":
System.out.print("" + (first * second));
break;
case "/":
System.out.print("" + (first / second));
break;
default: System.out.print("not sum, difference, product or quotient");
}
In that case where operator = "+";
, the output is 20
. If operator = "-";
, the output is -6
.
Upvotes: 2
Reputation: 425013
Remove everything else:
String operator = fileContent.replaceAll("[^-+*/]", "");;
Note that the minus sign must come first or last in the character class or be escaped, otherwise it's a range operator.
Upvotes: 2