Click to See Complete Forum and Search --> : Finding number position
hugo84
September 12th, 2009, 02:53 AM
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
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;
holestary
September 12th, 2009, 03:36 AM
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..
holestary
September 12th, 2009, 03:41 AM
String phrase = Integer.toString(f);
String reverse = new StringBuffer(phrase).reverse().toString();u can reverse like this..
hugo84
September 12th, 2009, 03:51 AM
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.
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;
holestary
September 12th, 2009, 03:54 AM
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:
414321
4
hugo84
September 12th, 2009, 04:24 AM
thanks man learnt alot from you.
jcaccia
September 12th, 2009, 01:06 PM
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
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):
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.
nuzzle
September 12th, 2009, 01:18 PM
thanks man learnt alot from you.
This is an alternative (if you by reverse order mean from the right to the left),
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%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.
jcaccia
September 12th, 2009, 01:47 PM
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:
if (Math.abs(f)%10 == digit) ...
nuzzle
September 12th, 2009, 02:33 PM
When f is negative this code always returns 0. The test in the if should be:
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;
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.