Connecting Tech Pros Worldwide Help | Site Map

Java Exceptions

Newbie
 
Join Date: Sep 2007
Posts: 16
#1: Nov 28 '07
Hi,

Here is the code - The program does not provide any message to the user when no argument is pass

Expand|Select|Wrap|Line Numbers
  1. public class Hexnumbers{
  2.  
  3.  
  4.  public static int conv1(String s) 
  5.  throws NumberFormatException {
  6.  
  7.   String digits = "0123456789ABCDEF";
  8.     s = s.toUpperCase();
  9.    int val = 0;
  10.    for (int i = 0; i < s.length(); i++) {
  11.       char c = s.charAt(i);
  12.         int d = digits.indexOf(c);
  13.        val += d*Math.pow(16, s.length()-1-i);
  14.  
  15.   }
  16.        return val;
  17.  
  18.  
  19. }
  20.  
  21.  
  22.    public static String conv2(int d)
  23.    throws NumberFormatException
  24.  {
  25.        String digits = "0123456789ABCDEF";
  26.        if (d == 0) return "0";
  27.        String hex = "";
  28.        while (d > 0) {
  29.         int digit = d % 16;                
  30.            hex = digits.charAt(digit) + hex;  
  31.            d = d / 16;
  32.        }
  33.       return hex;
  34.    }
  35.  
  36.  
  37.  
  38.     public static void main(String[] args) {
  39.  
  40. try{
  41.         int sum = 0;
  42.  
  43.       for (int i = 0; i < args.length; i++) {
  44.         System.out.println(args[i]);
  45.  
  46.  
  47.  
  48.  
  49.   int decimal = conv1(args[i]);
  50.  
  51.  
  52.  
  53.    System.out.println("Here is the decimal number = "  +  decimal);
  54.    sum += decimal;
  55.  
  56.      String hex = conv2( decimal);
  57.        System.out.println("Here is the Hex number  = " + hex);
  58.  
  59.         System.out.println("Here is the sum of **Hex** + **Decimal** ==  "  + sum);
  60.    }
  61.  
  62.   }catch(NumberFormatException nfe) {
  63.         System.out.println ("Please Enter a valid argument");
  64.  
  65.     }
  66.  
  67.  
  68.     }
  69. }
  70.  
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Nov 28 '07

re: Java Exceptions


Quote:

Originally Posted by Jromero

Hi,

Here is the code - The program does not provide any message to the user when no argument is pass

Yep, that is correct: you didn't tell it to do so,

kind regards,

Jos
Reply