Hello below I have a code and Im trying to make it so it cat
Hello, below I have a code and I\'m trying to make it so it cathches all possible exceptions. I tried to use a try catch but now I;m gettiing two errors. Before I added the try catch to the program it ran correctly. Can you tell me what those two errors mean? Also this is a Java program.
import java.util.Scanner;
/**
* Demonstrates the use of a stack to evaluate postfix expressions.
*
* @author Lewis and Chase
* @version 4.0
*/
public class PostfixTester
{
/**
* Reads and evaluates multiple postfix expressions.
*/
public static void main(String[] args)
{
try{
String expression, again;
int result;
Scanner in = new Scanner(System.in);
do
{
PostfixEvaluator evaluator = new PostfixEvaluator();
System.out.println(\"Enter a valid post-fix expression one token \" +
\"at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)\");
System.out.println(\"Each token must be an integer or an operator (+,-,*,/)\");
expression = in.nextLine();
result = evaluator.evaluate(expression);
System.out.println();
System.out.println(\"That expression equals \" + result);
Solution
Hi Friend, Error is not because of addition of try catch block. It is because \'PostfixEvaluator\' class is not availavle in same folder. Make sure that PostfixEvaluator class should be inported in your PostfixTester class.
import java.util.Scanner;
/**
* Demonstrates the use of a stack to evaluate postfix expressions.
*
* @author Lewis and Chase
* @version 4.0
*/
public class PostfixTester
{
/**
* Reads and evaluates multiple postfix expressions.
*/
public static void main(String[] args)
{
try{
String expression, again;
int result;
Scanner in = new Scanner(System.in);
do
{
PostfixEvaluator evaluator = new PostfixEvaluator();
System.out.println(\"Enter a valid post-fix expression one token \" +
\"at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)\");
System.out.println(\"Each token must be an integer or an operator (+,-,*,/)\");
expression = in.nextLine();
result = evaluator.evaluate(expression);
System.out.println();
System.out.println(\"That expression equals \" + result);
System.out.print(\"Evaluate another expression [Y/N]? \");
again = in.nextLine();
System.out.println();
}
while (again.equalsIgnoreCase(\"y\"));
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}

