NumberFormatExecption

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tudyfruity18
    New Member
    • Sep 2007
    • 8

    #1

    NumberFormatExecption

    I'm not familiar with Exception Handling and I don't know where to start. Im suppose to write a program with an exception handler that deals with non-numeric operands; then write another program without using an exception handler to achieve the same objective. My program should display a message that informs the user of the wrong operand type before exiting.

    Here's what I got so far:

    [CODE=java]public class Calculator {
    /** Main method */
    public static void main(String[] args) {
    // Check command-line arguments
    if (args.length != 3) {
    System.out.prin tln(
    " Please Use java operand1 operator operand2");
    System.exit(0);
    }

    // The result of the operation
    int result = 0;

    // Determine the operator
    switch (args[1].charAt(0)) {
    case '+': result = Integer.parseIn t(args[0]) +
    Integer.parseIn t(args[2]);
    break;
    case '-': result = Integer.parseIn t(args[0]) -
    Integer.parseIn t(args[2]);
    break;
    case '*': result = Integer.parseIn t(args[0]) *
    Integer.parseIn t(args[2]);
    break;
    case '/': result = Integer.parseIn t(args[0]) /
    Integer.parseIn t(args[2]);
    }

    // Display result
    System.out.prin tln(args[0] + ' ' + args[1] + ' ' + args[2]
    + " = " + result);
    }
    }[/CODE]
    Last edited by Ganon11; Oct 26 '07, 02:11 AM. Reason: Please use the [CODE] tags provided.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by tudyfruity18
    I'm not familiar with Exception Handling and I don't know where to start. Im suppose to write a program with an exception handler that deals with non-numeric operands; then write another program without using an exception handler to achieve the same objective. My program should display a message that informs the user of the wrong operand type before exiting.

    Here's what I got so far:

    [CODE=java]public class Calculator {
    /** Main method */
    public static void main(String[] args) {
    // Check command-line arguments
    if (args.length != 3) {
    System.out.prin tln(
    " Please Use java operand1 operator operand2");
    System.exit(0);
    }

    // The result of the operation
    int result = 0;

    // Determine the operator
    switch (args[1].charAt(0)) {
    case '+': result = Integer.parseIn t(args[0]) +
    Integer.parseIn t(args[2]);
    break;
    case '-': result = Integer.parseIn t(args[0]) -
    Integer.parseIn t(args[2]);
    break;
    case '*': result = Integer.parseIn t(args[0]) *
    Integer.parseIn t(args[2]);
    break;
    case '/': result = Integer.parseIn t(args[0]) /
    Integer.parseIn t(args[2]);
    }

    // Display result
    System.out.prin tln(args[0] + ' ' + args[1] + ' ' + args[2]
    + " = " + result);
    }
    }[/CODE]
    There's an article here with an introduction to exception handling.

    Comment

    Working...