All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.pdx.cs410J.lang.DivTwo Maven / Gradle / Ivy

The newest version!
package edu.pdx.cs410J.lang;

/**
 * This class demonstrates throwing exceptions.  It reads two
 * doubles from the command line and divides the second
 * by the first.  If the second is zero, an
 * IllegalArgumentException is thrown.
 */
public class DivTwo {

  /**
   * Reads two doubles from the command line and divides
   * the first by the second.
   */
  public static void main(String[] args) {
    double d1 = 0.0;
    double d2 = 0.0;

    if (args.length < 2) {
      System.err.println("Not enough arguments");
      System.exit(1);
    }

    try {
      d1 = Double.parseDouble(args[0]);
      d2 = Double.parseDouble(args[1]);

    } catch (NumberFormatException ex) {
      System.err.println("Not a double: " + ex);
      System.exit(1);
    }

    if (d2 == 0.0) {
      String m = "Denominator can't be zero!";
      throw new IllegalArgumentException(m);
    }

    System.out.println(d1 + " / " + d2 + " = " + (d1/d2));
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy