Calculating the sum of an array with decimal & hexadecimal elements.

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

    Calculating the sum of an array with decimal & hexadecimal elements.

    I need to add the sum of hex and decimal
    right now is not adding anything

    A5
    165
    A5
    165A50
    FAA
    4010
    FAA
    4010FAA0
    Here is my code
    [code=java]
    public class Numbers{

    public static int Numbers(String s) {
    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 = 16 * val + d;
    }
    return val;
    }

    public static String Numbers(int d) {
    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) {
    int sum = 0;

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

    int decimal = Numbers(args[i]);
    System.out.prin tln(decimal );

    String hex = Numbers(decimal );
    System.out.prin tln(hex);

    System.out.prin tln( decimal + hex + sum );
    }
    }
    }
    [/code]
    Last edited by Ganon11; Oct 5 '07, 07:02 PM. Reason: Fixing [CODE] tags.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Well, you haven't added a method to add anything. Think about how you'll do this: especially of concern is, how will you tell if a number is base 16 (hex) or base 10 (decimal)?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Jromero
      I need to add the sum of hex and decimal
      right now is not adding anything
      It's ambiguous: does 42 the decimal number 42 or the hexadecimal representation
      of the numer 66 (in decimal)?

      kind regards,

      Jos

      Comment

      • Jromero
        New Member
        • Sep 2007
        • 16

        #4
        Originally posted by JosAH
        It's ambiguous: does 42 the decimal number 42 or the hexadecimal representation
        of the numer 66 (in decimal)?

        kind regards,

        Jos
        Here is my code
        [code=java]
        //LabAssignment2
        //Jessica Romero -10-05-07
        public class Numbers{

        public static int conv1(String s) {
        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) {
        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) {
        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(decimal );

        String hex = conv2(decimal);
        System.out.prin tln(hex);



        }
        }
        }[/code]
        Last edited by pbmods; Oct 5 '07, 09:51 PM. Reason: Added CODE tags.

        Comment

        • Jromero
          New Member
          • Sep 2007
          • 16

          #5
          Originally posted by Ganon11
          Well, you haven't added a method to add anything. Think about how you'll do this: especially of concern is, how will you tell if a number is base 16 (hex) or base 10 (decimal)?
          I have hard time with arrays can you please help me
          [code=java]
          public static int conv1(String s) {
          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);[/code]
          Last edited by pbmods; Oct 5 '07, 09:51 PM. Reason: Added CODE tags.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Yes, we can help you, but you haven't done anything to even try to add. Please take a look at our posting guidelines (click Help at the top of your screen, click Posting Guidelines, and read through all of them).

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Jessica.

              Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

              Please use CODE tags when posting source code:

              &#91;CODE=ja va]
              Java code goes here.
              &#91;/CODE]

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by pbmods
                Heya, Jessica.

                Changed thread title to better describe the problem
                If that is the correct topic title then all I can say is: read my reply #3 again.

                kind regards,

                Jos

                Comment

                Working...