• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

Add Exception Handler

I am trying to include exception handling in:

    public static int hexToDecimal(char ch)  {
       
        int temp=0;
        try{
           
            if (ch<'0' || ch>'F')
                throws new NumberFormatException("not a hex number.");
               
            if (ch>='A' && ch <='F')
                temp = 10+ch-'A';
            else
                temp = ch-'0';
       
        }catch{
           
           
        }
        return temp;
    }

for when the char is not hex.

The complete code is included below.

Thank you.
import java.util.Scanner;

public class Exercise13_6{

    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
            
        
        System.out.print("Enter a hex number: ");
        String hex=input.nextLine();
        
        System.out.println("The decimal value for hex number "
                + hex + " is " + hexToDecimal(hex.toUpperCase()));
    }
    
    public static int hexToDecimal(String hex) {
        int decimalValue=0;
        for(int i=0; i<hex.length();i++) {
            char hexChar=hex.charAt(i);
            decimalValue=decimalValue*16+hexToDecimal(hexChar);
        }
            return decimalValue;
    }
    
    public static int hexToDecimal(char ch)  {
        
        int temp=0;
        try{
            
            if (ch<'0' || ch>'F')
                throws new NumberFormatException("not a hex number.");
                
            if (ch>='A' && ch <='F')
                temp = 10+ch-'A';
            else
                temp = ch-'0';
        
        }catch{
            
            
        }
        return temp;
    }
}

Open in new window

0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 5
1 Solution
 
for_yanCommented:
should be not "throws..."  but "throw"
0
 
for_yanCommented:
"throws" is in method declaration

"throw" when you need to throw exception
0
 
for_yanCommented:


     }catch{


should be

   } catch(Exception ex) {

(or maybe catch(NumberFormatException ex) {

but not

} catch {
 without Exception parentheses

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:

compiling and throwing exception:
import java.util.Scanner;

public class Exercise13_6{

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);


        System.out.print("Enter a hex number: ");
        String hex=input.nextLine();

        System.out.println("The decimal value for hex number "
                + hex + " is " + hexToDecimal(hex.toUpperCase()));
    }

    public static int hexToDecimal(String hex) {
        int decimalValue=0;
        for(int i=0; i<hex.length();i++) {
            char hexChar=hex.charAt(i);
            decimalValue=decimalValue*16+hexToDecimal(hexChar);
        }
            return decimalValue;
    }

    public static int hexToDecimal(char ch)  {

        int temp=0;
        try{

            if (ch<'0' || ch>'F')
                throw new NumberFormatException("not a hex number.");

            if (ch>='A' && ch <='F')
                temp = 10+ch-'A';
            else
                temp = ch-'0';

        }catch (NumberFormatException ex){
                         ex.printStackTrace();

        }
        return temp;
    }
}

Open in new window



Enter a hex number: oy
java.lang.NumberFormatException: not a hex number.
	at Exercise13_6.hexToDecimal(Exercise13_6.java:32)
	at Exercise13_6.hexToDecimal(Exercise13_6.java:21)
	at Exercise13_6.main(Exercise13_6.java:13)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
java.lang.NumberFormatException: not a hex number.
	at Exercise13_6.hexToDecimal(Exercise13_6.java:32)
	at Exercise13_6.hexToDecimal(Exercise13_6.java:21)
	at Exercise13_6.main(Exercise13_6.java:13)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
The decimal value for hex number oy is 0

Open in new window

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you for the solution.

re:> The decimal value for hex number oy is 0

Is it possible not to print this message when there is an error?
0
 
for_yanCommented:

import java.util.Scanner;

public class Exercise13_6{

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);


        System.out.print("Enter a hex number: ");
        String hex=input.nextLine();
        boolean good = true;
        int num = -100000;
                           try{
        num =  hexToDecimal(hex.toUpperCase());
                           }catch(Exception ex){
                               System.out.println("error");
                            good = false;
                           }
                             if(good)
        System.out.println("The decimal value for hex number "
                + hex + " is " + num);
    }

    public static int hexToDecimal(String hex) throws NumberFormatException {
        int decimalValue=0;
        for(int i=0; i<hex.length();i++) {
            char hexChar=hex.charAt(i);
            decimalValue=decimalValue*16+hexToDecimal(hexChar);
        }
            return decimalValue;
    }

    public static int hexToDecimal(char ch) throws NumberFormatException {

        int temp=0;
        try{

            if (ch<'0' || ch>'F')
                throw new NumberFormatException("not a hex number.");

            if (ch>='A' && ch <='F')
                temp = 10+ch-'A';
            else
                temp = ch-'0';

        }catch (NumberFormatException ex){
                         throw ex;

        }
        return temp;
    }
}

Open in new window


Output:
Enter a hex number: 0y
error

Open in new window



0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now