I am trying to parse a string with both negative numbers and minus signs and add each token to a queue called infix and a function to tell if the token is an operator. However the negative gets treated like a minus sign and is added to queue separate from the negative number. Here is my code to break the string into numbers and add them to a queue.
for(int i = 0;i < expression.length();i++) // add all items from the expression to the infix queue
{
String value = String.valueOf(expression.charAt(i));
if(eval.isOperator(value) == true)
{
infix.add(value);
}
else
{
for(int j = i+1; eval.isOperator(String.valueOf(expression.charAt(j))) == false; j++)//add token to value until an operator is found and that number ends
{
value += expression.charAt(j);
i++;
}
infix.add(value);
}
}
and the eval class
public class Eval
{
public boolean isOperator(String n)
{
switch(n)
{
case "(":
return true;
case ")":
return true;
case "*":
return true;
case "/":
return true;
case "+":
return true;
case "-":
return true;
case "#":
return true;
}
return false;
}
public int Priority(String op)
{
switch(op)
{
case "(":
return 3;
case "*":
return 2;
case "/":
return 2;
case "+":
return 1;
case "-":
return 1;
}
return 0;
}
}
}
infix
? – user2201041-
as an infix operator? If you just think of it as just a part of a number (operand), and automatically add a+
operator if you see a positive number followed by a negative number, it should work fine. – Sweeper