Hello all.
I have a small problem, i need to recieve a 8 digit number for example:
12345678
87654321
and so on.
After i need to devide the value and use the int as a array, something like
int i=123;
System.out.println(i[1]);
It should return 2 (therocially), but obviously the doesnt compile.
I was thinking on making the array of ints, but to read it i will need spaces and that is not acceptable, was already thinking about working with pointers, but found out that java doesnt have pointers.
Any ideias?

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]);

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]

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 ?

Yes, the ideia is to use, or find a better way:
Scanner s_input = new Scanner(System.in);
i_input=s_input.nextInt();
Hmm, now i'm thinking on forgetting about the int and start by reading the number as string, after loop the size and check if all its elements are numeric, but i think it would be considered cheating =)

Here's a possible solution. Not sure what you are trying to accomplish, but this sounds like it should do it. There are probably better options.

int i;
String myNumberAsString;
myNumberAsString = Integer.toString(i);

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html#toString(int)

Then you can use the String function "charAt".

int indexNumber;
char desiredDigit;
String myDigitStr;
int myDigitInt;

desiredDigit = myNumberAsString.charAt(indexNumber);
//optional: convert back to an int
//convert back to String first
myDigitStr = Character.toString(desiredNumber);
//then, convert String back to int
myDigitInt = Integer.parseInt(myDigitStr);

System.out.println(desiredDigit);
commented: Thanks =) +1

Just modulus (%) by ten. That will give you the last digit.

Thanks for all the help.
The solution what i was looking for was that cgeier provided.

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.

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).

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.

I could mark it unsolved if neden =)
Shure, after having the application working, i was rereading the post to learn more.
And I dindt put your post down, i've just upped the cgeier post, if it would make you more glad your post could be upped also =).
And i was thinking on making the calculation first and for future use, create variables for everything and not count the neden number many times.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.