|
-
September 12th, 2009, 02:53 AM
#1
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.
-
September 12th, 2009, 03:36 AM
#2
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 
-
September 12th, 2009, 03:41 AM
#3
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 
-
September 12th, 2009, 03:51 AM
#4
Re: Finding number position
 Originally Posted by holestary
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;
-
September 12th, 2009, 03:54 AM
#5
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:
Please use code tags like this : [SIGPIC][/SIGPIC]
Code Your Dreams 
-
September 12th, 2009, 04:24 AM
#6
Re: Finding number position
thanks man learnt alot from you.
-
September 12th, 2009, 01:06 PM
#7
Re: Finding number position
 Originally Posted by hugo84
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.
-
September 12th, 2009, 01:18 PM
#8
Re: Finding number position
 Originally Posted by hugo84
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%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.
-
September 12th, 2009, 01:47 PM
#9
Re: Finding number position
 Originally Posted by nuzzle
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) ...
-
September 12th, 2009, 02:33 PM
#10
Re: Finding number position
 Originally Posted by jcaccia
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|