Working with integers

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster

Working with integers

 
0
  #1
Oct 21st, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #2
Oct 21st, 2009
Originally Posted by jen140 View Post
int i=123;
System.out.println(i[1]);
Any ideias?
'i' is an int, not an array:
  1. int [] i = new int[2];
  2. i[0] = 1;
  3. i[1] = 2;
  4. i[2] = 3;
  5. System.out.println(i[1]);
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso
 
0
  #3
Oct 21st, 2009
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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster
 
0
  #4
Oct 21st, 2009
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 =)
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 103
Reputation: cgeier is an unknown quantity at this point 
Solved Threads: 14
cgeier cgeier is offline Offline
Junior Poster
 
1
  #5
Oct 21st, 2009
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.

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

  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,654
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #6
Oct 21st, 2009
Just modulus (%) by ten. That will give you the last digit.
Out.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster
 
0
  #7
Oct 22nd, 2009
Thanks for all the help.
The solution what i was looking for was that cgeier provided.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,654
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #8
Oct 22nd, 2009
Originally Posted by BestJewSinceJC View Post
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:

  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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster
 
0
  #9
Oct 22nd, 2009
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).
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,654
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #10
Oct 22nd, 2009
Originally Posted by jen140 View Post
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.
Out.
Reply With Quote Quick reply to this message  
Reply

Tags
int, integer, java

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 982 | Replies: 10
Thread Tools Search this Thread



Tag cloud for int, integer, java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC