Hi Tammy,I think it is as easy as adding :the class definiton 
NegativeValueEnteredException. another else if intEq[] < 0 throw new 
NegativeValueEnteredException at the top of the 'if' list. and another catch 
NegativeValueEnteredException with what you want to do when the negative value 
is caught at the bottom.Perhaps those with more Java experience can define a 
different way but this should work.DenS----- Original Message -----From: 
"Estes, Tammy" Date: Wednesday, October 22, 2008 11:32 amSubject: [java 
programming] Question About Lab 1007 Hex to Dec Your Own Exception 2To: Free 
Java Programming Online Training Course By Sang Shin > 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to