473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. public class Calculator {
  2.   /** Main method */
  3.   public static void main(String[] args) {
  4.     // Check command-line arguments
  5.     if (args.length != 3) {
  6.       System.out.println(
  7.         " Please Use java operand1 operator operand2");
  8.       System.exit(0);
  9.     }
  10.  
  11.     // The result of the operation
  12.     int result = 0;
  13.  
  14.     // Determine the operator
  15.     switch (args[1].charAt(0)) {
  16.       case '+': result = Integer.parseInt(args[0]) +
  17.                          Integer.parseInt(args[2]);
  18.                 break;
  19.       case '-': result = Integer.parseInt(args[0]) -
  20.                          Integer.parseInt(args[2]);
  21.                 break;
  22.       case '*': result = Integer.parseInt(args[0]) *
  23.                          Integer.parseInt(args[2]);
  24.                 break;
  25.       case '/': result = Integer.parseInt(args[0]) /
  26.                          Integer.parseInt(args[2]);
  27.     }
  28.  
  29.     // Display result
  30.     System.out.println(args[0] + ' ' + args[1] + ' ' + args[2]
  31.       + " = " + result);
  32.   }
  33. }
Oct 26 '07 #1
1 2405
r035198x
13,262 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. public class Calculator {
  2.   /** Main method */
  3.   public static void main(String[] args) {
  4.     // Check command-line arguments
  5.     if (args.length != 3) {
  6.       System.out.println(
  7.         " Please Use java operand1 operator operand2");
  8.       System.exit(0);
  9.     }
  10.  
  11.     // The result of the operation
  12.     int result = 0;
  13.  
  14.     // Determine the operator
  15.     switch (args[1].charAt(0)) {
  16.       case '+': result = Integer.parseInt(args[0]) +
  17.                          Integer.parseInt(args[2]);
  18.                 break;
  19.       case '-': result = Integer.parseInt(args[0]) -
  20.                          Integer.parseInt(args[2]);
  21.                 break;
  22.       case '*': result = Integer.parseInt(args[0]) *
  23.                          Integer.parseInt(args[2]);
  24.                 break;
  25.       case '/': result = Integer.parseInt(args[0]) /
  26.                          Integer.parseInt(args[2]);
  27.     }
  28.  
  29.     // Display result
  30.     System.out.println(args[0] + ' ' + args[1] + ' ' + args[2]
  31.       + " = " + result);
  32.   }
  33. }
There's an article here with an introduction to exception handling.
Oct 26 '07 #2

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.