Tammy:
Add a new class
class NegativeValueEnteredException extends RuntimeException {
}
then in your for loop add another if else
if else(currChar < '0')
throw new NegativeValueEnteredException();
then in your try/catch block add another catch statement
catch(NegativeValueEnteredException e){
JOptionPane.showMessageDialog(null," Your MESSAGE");}
that should do it.
Jeffrey
On Oct 22, 7:56 am, "Estes, Tammy" <[EMAIL PROTECTED]> wrote:
> Here's the code:
>
> import javax.swing.*;
>
> // This is my custom exception
> class InvalidHexException extends RuntimeException {
>
> }
>
> class HexToDec {
>
> // This method throws a custom exception called InvalidException
> static int [] convertToInt(String hex) throws InvalidHexException {
> char currChar;
> int intEq[] = new int[hex.length()];
> for (int i = 0; i < hex.length(); i++) {
> currChar = hex.charAt(i);
> if (currChar >= '0' && currChar <='9') {
> intEq[i] = currChar - '0';
> } else if (currChar >= 'a' && currChar <='f') {
> intEq[i] = currChar - 'a' + 10;
> } else if (currChar >= 'A' && currChar <='F' ) {
> intEq[i] = currChar - 'A' + 10;
> } else {
> throw new InvalidHexException();
> }
> }
> return intEq;
> }
>
> static int convertToDec(int intEq[]) {
> int result = 0;
> int mult = 1; //multiplier
> for (int j = intEq.length - 1; j >= 0; j--) {
> result += intEq[j]*mult;
> mult *= 16;
> }
> return result;
> }
>
> public static void main(String args[]) {
> String hex;
> int intEq[];
>
> // Receive hex value from a user
> hex = JOptionPane.showInputDialog(null, "Input hex: ");
> try {
>
> // convertToInt() method will throw InvalidHexExceptions if
> // the value is not in a valid format
> intEq = convertToInt(hex);
>
> Integer dec = new Integer(convertToDec(intEq));
>
> // Display the result
> JOptionPane.showMessageDialog(null, dec);
> System.exit(0);
> } catch (InvalidHexException e) {
> JOptionPane.showMessageDialog(null, "InvalidHexException is
> caught: Enter valid hex value");
> }
> System.exit(0);
> }
>
> }
>
> Here's the question:
>
> Modify HexToDec.java so that when a negative value such as -100 is
> entered, trigger a different exception called
> NegativeValueEnteredException
>
> They want us to modify this. How did you modify the lab? I'm curious
> what other people did to perfect this...
>
> Thanks,
>
> Tammy Estes
>
> Programmer Analyst
>
> DAFS/OIT
>
> Application Development and Management (ADAM)
>
> Tel: 207.624.8287
>
> Cell: 207.592.3710
>
> [EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---