Java Exceptions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jromero
    New Member
    • Sep 2007
    • 16

    #1

    Java Exceptions

    Hi,

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

    [code=java]
    public class Hexnumbers{


    public static int conv1(String s)
    throws NumberFormatExc eption {

    String digits = "0123456789ABCD EF";
    s = s.toUpperCase() ;
    int val = 0;
    for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    int d = digits.indexOf( c);
    val += d*Math.pow(16, s.length()-1-i);

    }
    return val;


    }


    public static String conv2(int d)
    throws NumberFormatExc eption
    {
    String digits = "0123456789ABCD EF";
    if (d == 0) return "0";
    String hex = "";
    while (d > 0) {
    int digit = d % 16;
    hex = digits.charAt(d igit) + hex;
    d = d / 16;
    }
    return hex;
    }



    public static void main(String[] args) {

    try{
    int sum = 0;

    for (int i = 0; i < args.length; i++) {
    System.out.prin tln(args[i]);




    int decimal = conv1(args[i]);



    System.out.prin tln("Here is the decimal number = " + decimal);
    sum += decimal;

    String hex = conv2( decimal);
    System.out.prin tln("Here is the Hex number = " + hex);

    System.out.prin tln("Here is the sum of **Hex** + **Decimal** == " + sum);
    }

    }catch(NumberFo rmatException nfe) {
    System.out.prin tln ("Please Enter a valid argument");

    }


    }
    }
    [/code]
    Last edited by JosAH; Nov 28 '07, 08:59 PM. Reason: correct [code=java] ...[/code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    Working...