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

    Finding number position

    Hi i have a problem here im reading 2 int values f and digit from a user and i want to count the position of the number he input in a reverse order starting from 1. i Kept getting -1 from this. I think the problem is because of INT mismatch with String?Or can i use indexOfSubList? how can i implement it

    Code:
    public static int pos(int f, int digit){
          
             StringBuffer phrase = new StringBuffer(f);
             phrase.reverse();
             String x=Integer.toString(digit);
             int firstoccur = phrase.indexOf(x); // find where the Digit x first occurs
            
             return firstoccur;
    Last edited by hugo84; September 12th, 2009 at 08:19 AM.

  2. #2
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Re: Finding number position

    Code:
    public static int pos(int f, int digit){
          
             StringBuffer phrase = new StringBuffer(f);
             phrase.reverse();
             String x=Integer.toString(digit);
             int firstoccur = phrase.indexOf(x); // find where the Digit x first occurs
            
             return firstoccur;
    i dont think it makes f a string..

    Please use code tags like this :
    [SIGPIC][/SIGPIC]
    Code Your Dreams

  3. #3
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Re: Finding number position

    Code:
            String phrase = Integer.toString(f);
            String reverse = new StringBuffer(phrase).reverse().toString();
    u can reverse like this..

    Please use code tags like this :
    [SIGPIC][/SIGPIC]
    Code Your Dreams

  4. #4
    Join Date
    Aug 2009
    Posts
    52

    Re: Finding number position

    Quote Originally Posted by holestary View Post
    Code:
            String phrase = Integer.toString(f);
            String reverse = new StringBuffer(phrase).reverse().toString();
    u can reverse like this..
    Hmmm.Weird. its not reversing the order from the results of my output.

    Code:
           public static int position(int f, int digit){
             String phrase = Integer.toString(f);
             String reverse = new StringBuffer(phrase).reverse().toString();
             String x=Integer.toString(digit);
             int firstoccur = phrase.indexOf(x); // find where the Digit x first occurs from the right
            
             return firstoccur;

  5. #5
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Re: Finding number position

    Code:
    public class position {
    
        public static int pos(int f, int digit){
              
            String phrase = Integer.toString(f);
            String reverse = new StringBuffer(phrase).reverse().toString();
            System.out.println(reverse);
            String x=Integer.toString(digit);
            int firstoccur = reverse.indexOf(x); // find where the Digit x first occurs
            System.out.print(firstoccur+1);  //because index starts 0
            return firstoccur;
            }
           public static void main(String[] args) {
         
            pos(123414,3);       
            
        }
    
    }
    i think u understand what i mean with codes..
    output:
    Code:
    414321
    4

    Please use code tags like this :
    [SIGPIC][/SIGPIC]
    Code Your Dreams

  6. #6
    Join Date
    Aug 2009
    Posts
    52

    Re: Finding number position

    thanks man learnt alot from you.

  7. #7
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Finding number position

    Quote Originally Posted by hugo84 View Post
    Hi i have a problem here im reading 2 int values f and digit from a user and i want to count the position of the number he input in a reverse order starting from 1. i Kept getting -1 from this. I think the problem is because of INT mismatch with String?Or can i use indexOfSubList? how can i implement it

    Code:
    public static int pos(int f, int digit){
          
             StringBuffer phrase = new StringBuffer(f);
             phrase.reverse();
             String x=Integer.toString(digit);
             int firstoccur = phrase.indexOf(x); // find where the Digit x first occurs
            
             return firstoccur;
    Let's see if I understood the question: given an integer number and a single digit you want to know the first position of that digit in the number from right to left, correct?
    I think this gives you the answer (-1 means the digit is not in the number):
    Code:
    private static int pos (int f, int digit) {
    	String s = Integer.toString(f);
    	int p = s.lastIndexOf('0' + digit);
    	return (p < 0 ? p : s.length() - p);
    }
    StringBuffer phrase = new StringBuffer(f); does not convert f to String; it creates a StringBuffer with a capacity to hold f characters.
    Last edited by jcaccia; September 12th, 2009 at 01:16 PM.

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

    Re: Finding number position

    Quote Originally Posted by hugo84 View Post
    thanks man learnt alot from you.
    This is an alternative (if you by reverse order mean from the right to the left),

    Code:
    public static int pos(int f, int digit) {
       if (f==0 && digit==0) return 1; // handle special case
       int pos = 1;
       while (f != 0) {
           if (f&#37;10 == digit) return pos; // check rightmost digit of f
           f = f/10; // skip rightmost digit of f
           pos++;
       }
       return 0;
    }
    Digits from f are extracted from the right to the left. If none matches digit, 0 is returned.

    Apart from being fast this code has the advantage of working with negative f.
    Last edited by nuzzle; September 12th, 2009 at 01:24 PM.

  9. #9
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Finding number position

    Quote Originally Posted by nuzzle View Post
    Apart from being fast this code has the advantage of working with negative f.
    When f is negative this code always returns 0. The test in the if should be:
    Code:
    if (Math.abs(f)%10 == digit) ...

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

    Re: Finding number position

    Quote Originally Posted by jcaccia View Post
    When f is negative this code always returns 0. The test in the if should be:
    Code:
    if (Math.abs(f)%10 == digit) ...
    Okay, you're right. I don't know why I claimed that. I think it's because one common use of this code is to count the digits in a number and that works for negative numbers too.

    So this code is just a fast alternative. If you want it to work with negative f you can put in this line in the function before the loop starts,

    if (f<0) f = -f;

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