944,094 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2123
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 21st, 2009
0

Working with integers

Expand Post »
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?
Similar Threads
Reputation Points: 11
Solved Threads: 6
Junior Poster
jen140 is offline Offline
116 posts
since Jan 2009
Oct 21st, 2009
0
Re: Working with integers
Click to Expand / Collapse  Quote originally posted by jen140 ...
int i=123;
System.out.println(i[1]);
Any ideias?
'i' is an int, not an array:
Java Syntax (Toggle Plain Text)
  1. int [] i = new int[2];
  2. i[0] = 1;
  3. i[1] = 2;
  4. i[2] = 3;
  5. System.out.println(i[1]);
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Oct 21st, 2009
0
Re: Working with integers
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]
Last edited by tux4life; Oct 21st, 2009 at 5:00 pm.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Oct 21st, 2009
0
Re: Working with integers
Quote ...
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 =)
Reputation Points: 11
Solved Threads: 6
Junior Poster
jen140 is offline Offline
116 posts
since Jan 2009
Oct 21st, 2009
1
Re: Working with integers
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.

Java Syntax (Toggle Plain Text)
  1. int i;
  2. String myNumberAsString;
  3. myNumberAsString = Integer.toString(i);

http://java.sun.com/j2se/1.4.2/docs/...#toString(int)

Then you can use the String function "charAt".

Java Syntax (Toggle Plain Text)
  1. int indexNumber;
  2. char desiredDigit;
  3. String myDigitStr;
  4. int myDigitInt;
  5.  
  6. desiredDigit = myNumberAsString.charAt(indexNumber);
  7. //optional: convert back to an int
  8. //convert back to String first
  9. myDigitStr = Character.toString(desiredNumber);
  10. //then, convert String back to int
  11. myDigitInt = Integer.parseInt(myDigitStr);
  12.  
  13. System.out.println(desiredDigit);
Last edited by cgeier; Oct 21st, 2009 at 9:57 pm.
Reputation Points: 41
Solved Threads: 15
Junior Poster
cgeier is offline Offline
104 posts
since Oct 2008
Oct 21st, 2009
0
Re: Working with integers
Just modulus (%) by ten. That will give you the last digit.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 22nd, 2009
0
Re: Working with integers
Thanks for all the help.
The solution what i was looking for was that cgeier provided.
Reputation Points: 11
Solved Threads: 6
Junior Poster
jen140 is offline Offline
116 posts
since Jan 2009
Oct 22nd, 2009
0
Re: Working with integers
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:

Java Syntax (Toggle Plain Text)
  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. int myInt = 123;
  4. int newInt = myInt%10;
  5. System.out.println(newInt);
  6. newInt = myInt/=10;
  7. System.out.println(newInt%10);
  8. }

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.
Last edited by BestJewSinceJC; Oct 22nd, 2009 at 1:46 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 22nd, 2009
0
Re: Working with integers
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).
Reputation Points: 11
Solved Threads: 6
Junior Poster
jen140 is offline Offline
116 posts
since Jan 2009
Oct 22nd, 2009
0
Re: Working with integers
Click to Expand / Collapse  Quote originally posted by jen140 ...
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.
Last edited by BestJewSinceJC; Oct 22nd, 2009 at 8:19 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java program help/advice
Next Thread in Java Forum Timeline: Help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC