Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Lewis Evans   on Feb 01 In Java Category.

  
Question Answered By: Dirck Jansen   on Feb 01

1. you have to make the CalculatorException class.

public class CalculatorException extends exception  {
public CalculatorException() {
super();
}

public CalculatorException(String errorMsg) {
super(errorMsg);
}
}

2. To make things easier, add  throws in several methods to
propagate the exception to the method which will catch it.
public static String calc(String input) throws  CalculatorException {
...}
public static String calculation (Vector ParsedsEquation) throws
CalculatorException {...}
public static String computation (String a, char op, String b)
throws CalculatorException{...}

3. change the printResult() to be able to catch the
CalculatorException

public static void printResult() {
try {
System.out.print("\n\nAnswer of " +sEquation + " is: ");
System.out.println(calc(sEquation));
} catch (CalculatorException ce) {
System.out.println(ce.getMessage());
}
}

4. To throws an exception saying "Only numbers  can be inputted!",
maybe you can check the String with Character.isDigit() method.
eg.

String s = "1234ga";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
System.out.println("Digit");
} else {
System.out.println("Non-digit");
}
}

Where to validate it, try to figure it out.

Share: 

 

This Question has 1 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Need help in code Or get search suggestion and latest updates.


Tagged: