我似乎无法崩溃我的程序以获得我想要的错误

时间:2012-05-16 09:12:00

标签: java

我的任务是试图抓住这个:

  • 如果用户未输入至少3个令牌
  • 如果用户没有为代币1和3输入有效数字。

我尝试过研究它,我似乎无法找到可以解决的例外情况。 现在我的例外似乎适用于上面的两个条件,但我的导师希望我有两个捕获,而不是一个。

我希望能够使这个程序崩溃,这样我最终可以获得另一个throw并捕获或修改这个程序,以便异常能够捕获一件事(例如,只有当用户没有输入至少3个令牌时)。 / p>

如果我尝试执行上述任何一项操作来破坏此程序,顺便说一下,它总是会给我我的错误消息,所以它显然似乎涵盖了我上面的两件事。

我的例外涵盖了我需要捕捉的两件事,但我需要两次,而不是一次。

import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Driver {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        StringTokenizer st;
        String input, tok1, tok2, tok3;
        double dtok1, dtok3;
        boolean loop = true;

        //input
        System.out.println("Simple calculator program");
        System.out.println("This program will only except a math expression with");
        System.out.println("2 operands and 1 operator. Exceptable operators");
        System.out.println("are +, -, *, /, and %. For example, 2*3 or 6%4.");
        while (loop) {
            System.out.print("\nplease enter an expression or q to quit: ");
            try {
                input = sc.next();

                if (input.equalsIgnoreCase("q")) {
                    loop = false;
                } else {
                    //calculations
                    st = new StringTokenizer(input,"+-*/%",true);
                    tok1 = st.nextToken();
                    tok2 = st.nextToken();
                    tok3 = st.nextToken();
                    dtok1 = Double.parseDouble(tok1);
                    dtok3 = Double.parseDouble(tok3);
                    System.out.println(input + "=" + 
                    singleExpression(dtok1,dtok3,tok2));
                }
            } catch (Exception NoSuchElementException) {
                System.out.println ("Error! Please enter 2 operands and 1 operator!");
            }
        }
    }

    static double singleExpression (double num1, double num2, String operator) {
        double output = 0.0;
        if (operator.equals("+")) {
            output = num1 + num2;
        } else if (operator.equals("-")) {
            output = num1 - num2;
        } else if (operator.equals("*")) {
            output = num1 * num2;
        } else if (operator.equals("/")) {
            output = num1 / num2;
        } else if (operator.equals("%")) {
            output = num1 % num2;
        } else {
            output = 0;
        }
        return output;
    }
}

2 个答案:

答案 0 :(得分:1)

st.nextToken() will throw a NoSuchElementException如果此tokenizer的字符串中没有其他标记。你已经看到了。

Double.parseDouble will throw a NumberFormatException如果字符串不包含可解析的double。

2条评论:

答案 1 :(得分:1)

我认为你使用异常类作为变量名,它应该是Class var。

异常会捕获任何内容(数字和少数元素)

try{
..
}  catch (NoSuchElementException nse) {
    System.out.println ("Exception for the String Tokenizer");
}catch (NumberFormatException nfe) {
    System.out.println ("Exception for the Number format");
}
catch (Exception otherException) {
    System.out.println ( "Something else.. " + otherException.getMessage() );
}