CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Posts
    1

    Splitting number

    Helo,

    I am currently trying to figure out a way to create a number checker and one of the steps involves splitting up a number into it's separate digits. Here is an example of what I'm trying to do:

    given the number 56197245 take every other digit starting with the 5 on the left and double it. So that would give you 10, 2, 14, 8. You then have to add up all the digits involved so you would have 1+0+2+1+4+8.

    Any advice?

    As a side note I am very new to programming so if I could get an explanation with steps involved it would be most appreciated.

    Thanks

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Splitting number

    When you say 'given the number', how is this number represented, as a String (i.e. text), or a numeric type (int, Integer, etc) ?

    For a String, you can use the charAt(..) method to get alternate digits by their position, then get their numeric values using Integer.parseInt(..), then add them.

    For a numeric type, you could use use String.valueOf(..) to get a String representation and use the method above.

    Alternatively you could use a fancy maths algorithm...

    The fastest algorithm can frequently be replaced by one that is almost as fast and much easier to understand...
    D. Jones
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Splitting number

    Norm

  4. #4
    Join Date
    Jun 2011
    Posts
    2

    Re: Splitting number

    Hi, below is my code. I hope it will help.

    public int SplitAndCalculate(String number){
    int finalResult=0;
    String resultofFirstStep="";
    for(int i=0;i<number.length();i=i+2){
    resultofFirstStep += Integer.toString((number.charAt(i)-48)*2);
    }
    for(int j=0;j<resultofFirstStep.length();j++){
    finalResult+=(resultofFirstStep.charAt(j)-48);
    }
    return finalResult;
    }

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: Splitting number

    Quote Originally Posted by chaosbeaver View Post
    Hi, below is my code. I hope it will help.

    public int SplitAndCalculate(String number){
    int finalResult=0;
    String resultofFirstStep="";
    for(int i=0;i<number.length();i=i+2){
    resultofFirstStep += Integer.toString((number.charAt(i)-48)*2);
    }
    for(int j=0;j<resultofFirstStep.length();j++){
    finalResult+=(resultofFirstStep.charAt(j)-48);
    }
    return finalResult;
    }
    You're generating lots of unnecessary intermediate String objects. Since String is immutable each time you add to a String object a new String object must be created. The Java compiler is allowed to put in an optimization here (for example by substituting the String with a StringBuilder) but that's not guaranteed. Regardless, in my view one should select the most appropriate data structure for each situation and in this case it would be a StringBuilder.

    Looping twice is not necessary. It can all be done in one go (avoiding the String intermediates) like this,

    Code:
    public int SplitAndCalculate(String number){
       int finalResult=0;
       for(int i=0; i<number.length(); i=i+2) {
          int d = number.charAt(i) - '0'; // int (0 - 9) corresponding to char ('0' - '9')
          finalResult += (d<5) ? d+d : d+d-9; // double d and add the sum of the digits
       }
       return finalResult;
    }
    Last edited by nuzzle; June 15th, 2011 at 05:19 AM.

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Splitting number

    @chaosbeaver - what is this junky code you posted?
    How is that helping the OP learn how to solve problems and write programs?

    In your code, what is the magic number 48? Why do you subtract that from the other?
    If you do tricks in your code, you should document what you are doing.
    This code can confuse people.
    Norm

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: Splitting number

    Quote Originally Posted by Norm View Post
    @chaosbeaver - what is this junky code you posted?
    How is that helping the OP learn how to solve problems and write programs?
    Well, if it works it should be fine with you. Or as you so succinctly put it in another thread,

    "You only know something if you can write working code. "

  8. #8
    Join Date
    Jun 2011
    Posts
    2

    Re: Splitting number

    Quote Originally Posted by Norm View Post
    @chaosbeaver - what is this junky code you posted?
    How is that helping the OP learn how to solve problems and write programs?

    In your code, what is the magic number 48? Why do you subtract that from the other?
    If you do tricks in your code, you should document what you are doing.
    This code can confuse people.
    Well, I am new to java and also new to this forum. That is actually my first post here. I think you are right. Next time I will post code together with annotations.

    @cshoya
    //charAt() will get the ASCII value of the character. For numbers 0~9, their actual value= their ASCII value subtract 48.

  9. #9
    Join Date
    May 2009
    Posts
    2,413

    Re: Splitting number

    Quote Originally Posted by chaosbeaver View Post
    //charAt() will get the ASCII value of the character. For numbers 0~9, their actual value= their ASCII value subtract 48.
    The standard way to do this is to use '0' instead of 48, like

    number.charAt(i) - '0';

    But you're still assuming a specific order among the digits and you're assuming ASCII encoding (while Java is using Unicode characters).

    So a much safer bet is to use the isDigit() and digit() methods of Character for the char to int conversion. These can also be used to incorporate an error check into the code, namely whether the String really holds digits only.

  10. #10
    Join Date
    Jun 2011
    Location
    USA
    Posts
    10

    Re: Splitting number

    You can do this without converting to String.

    Hints:
    (num/10) >10 Checks if number is greater than 10.
    num&#37;10 Get the last digit of a number.

  11. #11
    Join Date
    Jun 2011
    Posts
    5

    Re: Splitting number

    First of all you have to find the length of each. If length is greater than 1 than you have to split it other wise put same in array.And splitted number also put in array after wards you just add all separated numbers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured