how could I throw an exception in this case
guys, I'm reading a formula from file like: 2 + 2 - ( 3 - 1 ) * 3 $, but I want to be able to throw an exception if there are two operands together like: 2 + 2 3 * 4 $ or if there are two operators together like: 2 + + 3 $.
here's what I have so far, the operator part the program detects it, but I'm having problem with two operands together. I would like to throw the exception where "Bad Formula!..." is
Code :
while(in.hasNext())
{
String next = in.next();
do
{
if (optor.isOperator(next))
{
processOptor(next);
oprandCount = 0;
}
else
{
if(oprandCount == 0)
{
oprand.push(Integer.parseInt(next));
strOut += next+" ";
oprandCount++;
}
else
{
System.out.println("Bad formula!...");
System.exit(0);
}
}
next = in.next();
if(next.equals("$"))
processOptor(next);
}while(!next.equals("$"));
Re: how could I throw an exception in this case
Quote:
Originally Posted by
mia_tech
I would like to throw the exception where "Bad Formula!..." is
Code :
...
else
{
// System.out.println("Bad formula!...");
// System.exit(0);
throw new Exception( "Bad formula, bad, bad!" );
...
How about that?