Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

digit

Status
Not open for further replies.

ayya

MIS
Feb 4, 2006
3
CA
How can you create a method that returns the value of the digit that is position places from the right in the decimal representation of n.

e.g

Enter Number: 763

What digit do you want? 0

The digit is 3

---------
Enter Number: 8574

What digit do you want? 2

The digit is 5
-----------
This is what i got so far. but it apparently doesn't work. is there anyway to do exponent to a assign value like (10e position)

class position1 {
public static void main(String[] args) {
int n, position;
System.out.println("number");
n=In.getInt();
System.out.println ("position");
position=In.getInt();
digit(n,position);
}
public static int digit(int n, int position) {

for (int i=1; i<=position; i++){
n=n/(int)(10e2);
position=n%( (int)( 10e2) );

System.out.print(position);
}


return position;

}
}
 
Please use code-tags, and indentation.
Do you mean something like that:
Code:
	public static int digit (int n, int position)
	{
		int val = n % 10;
		for (int i=1; i<=position; i++)
		{
			n /= 10;
			val = n % 10;
			// System.out.print (" " + val);
		}
		// System.out.println ();
		return val;
	}
Why do you use a double-notation 10e2, if you need an int?
And didn't you mean 10e1?
n = n / y; is equivalent to
n /= y;

Then you're modifying position inside the loop, which is part of the termination-condition - unwanted, I suspect.

My approach:
Code:
	public static int posInZahl (int zahl, int pos)
	{
		while (pos--> 0)
			zahl /=10;
		return zahl % 10;
	}

seeking a job as java-programmer in Berlin:
 
Thanks a lot for helping. but i don't really understand your code. What i am trying to do is just to allow the computer to output the exact single digit that user choose.
like this

Enter Number: 12345

What digit do you want? 3

The digit is 2

it is sorta like a decimal counter. i think i suppose to use exponent in order to acheive that. working for quite a while now. still can't get it =(
 
Take a pencil.
Start with a small value (210) and a small index (0) and go line by line through your code.
Which value is changig - did you expect that?
Code:
line i position n   remark
0    - 0        210
1    1 0        210 for-condition not met - leaving for
2    - -        -   -
3      0
right result, but by incident, because the last cipher is the position.
for n=123 the result should be 3, but is 0 again.

After fixing it for an easy case, use more sophisticated cases.
Have an special eye on the borders, so for 8574 for position 0 and 3.
What shall happen if the index is out of bounds? (n=8574, position=9)

seeking a job as java-programmer in Berlin:
 
A little crude, but...

Code:
//(1) turn the number into a String

String numString = "" + num;

//(2)positions in String are 0-based (1st character is index 0), so position 1 by your reasoning is index "position - 1" (or 0)

char digAtChar = numString.charAt(pos-1);

//(3a) convert back to String if you like:

String digAtString = Character(digAtChar).toString();

//(3b) ...or an integer

int digAtInt = Integer.parseInt(Character(digAtChar).toString());

Is this all you're asking?

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
You could do with a reversed StringBuffer, like this:

private static int getPosInValue(int value, int pos) throws Exception {
StringBuffer buf = new StringBuffer(Integer.toString(value));
buf.reverse();
return Integer.parseInt(Character.toString(buf.charAt(pos)));
}
 
Sorry, but I forgot to add that I like Stefan's solution
much better than mine!

Ciao, Tom
 
I didn't look at every example (or, I guess, read the original post closely enough). I saw this example:

Enter Number: 8574

What digit do you want? 2

The digit is 5

...and thought counting started from 1 at the left. I see I got it wrong. I even saw the other examples later that didn't jibe with my understanding of the problem, but I was unable to shift my paradigm (as it were) and assumed the OP must have made a typo in that particular instance! [blush]

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Don't reverse the String, just use the total length minus the chosen position.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top