int i=123;
System.out.println(i[1]);
Any ideias?
'i' is an int, not an array:
int [] i = new int[2];
i[0] = 1;
i[1] = 2;
i[2] = 3;
System.out.println(i[1]);
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
So actually you want your program to take a number as input, and you want a way to get back a given digit n in the number?
For example you have the number 56231012 , and you want a way to get a certain digit, for example the second, which is 1, or the third, which is 0 ?
This can be easily achieved by a method, you don't need an array for this purpose, but an array will also do fine.
[edit]
Just for the sake of clarity: you can't use the array notation in Java on anything but an array.
Since a primitive, like an integer variable isn't an array, you can't use array notation (= using square brackets) with it, so you'll have to break the number down into digits manually.
[/edit]
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
Just modulus (%) by ten. That will give you the last digit.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Just modulus (%) by ten. That will give you the last digit.
My response was perfectly valid. I don't see why it was down voted, but to elaborate:
public static void main(String[] args) {
// TODO Auto-generated method stub
int myInt = 123;
int newInt = myInt%10;
System.out.println(newInt);
newInt = myInt/=10;
System.out.println(newInt%10);
}
That concept can be extended as far as you want, i.e., you can get whatever digit you need by doing that X number of times.
edit: Also, sorry guys for upping this when it was marked solved, but it was frustrating to get down voted (with no explanation) when I was only trying to help.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
I didnt said that your solution is bad, but the easier for me is the cgeier solution, and it needs fewere variables to work (when number is big).
And I also said "Thanks for all help" it did included your solution, sorry for not making me clear (again).
In the future, the "down voting" feature should only be used when somebody says something rude or incorrect or both, not when they say something correct that you don't understand. In that case, you should just reply and ask them to elaborate. I'm not angry, just saying, it was frustrating to get down voted for seemingly no reason.
:)
Also, the algorithm to print out the ith digit in an integer only requires one variable of storage - the integer itself. All you need to do is divide by ten to the appropriate power, then use modulus and print it. The code I posted uses two variables, but even if the idea were extended (for an integer of arbitrary length), the number of variables needed would still be two. And while his solution might have been easier for you, you should consider any solution a learning opportunity.
Anyway, again, I'm upping a thread that's marked solved, so I digress. Lets leave it at that.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354