CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2006
    Posts
    144

    Integer Manipulation [solved]

    Hello,

    I need a method to manipulate an Integer. The input is an integer, I don't know how many digits, e.g. Input could be

    160345 or
    12947583749

    Now I need the first 2 or 3 digits of that number, if I need 2 or 3 digits depends on the value. In the example above the method should return

    160 for the first number, and 12 for the second number. Of course I can convert to a string and do a if-then-else ladder to figure out what I need, is there a better way?

    Regards,
    J.
    Last edited by joebar; March 25th, 2011 at 06:14 PM. Reason: solved

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

    Re: Integer Manipulation

    If the number of digits you select depends on the numeric value, I don't see any alternative to an 'if...else..' You haven't said how the digit selection depends on the value, so I can't suggest anything else.

    A prudent question is one-half of wisdom...
    F. Bacon
    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
    Aug 2006
    Posts
    144

    Re: Integer Manipulation

    for example:

    starts with
    20
    23
    40

    then take the frist 2 digits

    when starts with
    21
    50
    then take the first 3 digits

    for everything else ignore the input.

  4. #4
    Join Date
    Oct 2008
    Posts
    50

    Re: Integer Manipulation

    Not sure if it's the best way to do it, but you could do something like
    Code:
    public class MyClass
    {
          TreeSet<Integer> two_numbers;
          TreeSet<Integer> three_numbers;
    
          MyClass(){
                 two_numbers = new TreeSet<Integer>();
                 two_numbers.add(20);
                 two_numbers.add(23);
                 //etc.
    
                 three_numbers = new TreeSet<Integer>();
                 three_numbers.add(21);
                 //etc.
           }
    
          //I'm gonna return -1 if the number is ignored.
          int getFirstNumbers(int number)
          {
                if(number < 10)
                     return -1;
    
                if(number < 100){
                     if(two_numbers.contains(number))
                           return number;
                     else return -1;
                }
                
                 while(number > 999)
                        number = (int)(number/10);
                 int first2 = (int)(number/10);
                 if(two_numbers.contains(first2))
                       return first2;
                 if(three_numbers.contains(first2))
                       return number;
                 return -1;
          }
    }

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

    Re: Integer Manipulation

    Meh... if the selection doesn't depend on the absolute value, but just on the leading digits, I'd be inclined to convert to a String and avoid any messy and unnecessary division:
    Code:
    String getDigits(Integer input) {
    
        // this map should probably be a class member and filled elsewhere
        Map<String, Integer> digitMap = new HashMap<String, Integer>();
        digitMap.put("20", 2);
        digitMap.put("23", 2);
        digitMap.put("40", 2);
        digitMap.put("21", 3);
        digitMap.put("50", 3);
    
        String inStr = input.toString();
    
        Integer count = digitMap.get(inStr.substring(0,2));
    
        if (count == null || inStr.length() < count) {
            return "";  // or null, or better still, throw an exception
        }
        return inStr.substring(0, count);    
    }
    Note: limited error handling included.

    Weeks of programming can save you hours of planning...
    Anon.
    Last edited by dlorde; March 23rd, 2011 at 11:57 AM.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

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