| | |
Working with integers
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 106
Reputation:
Solved Threads: 5
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?
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?
0
#2 Oct 21st, 2009
'i' is an int, not an array:
Java Syntax (Toggle Plain Text)
int [] i = new int[2]; i[0] = 1; i[1] = 2; i[2] = 3; System.out.println(i[1]);
Check out my New Bike at my Public Profile at the "About Me" tab
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
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 ?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."
•
•
Join Date: Jan 2009
Posts: 106
Reputation:
Solved Threads: 5
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 ?
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 =)
•
•
Join Date: Oct 2008
Posts: 103
Reputation:
Solved Threads: 14
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.
http://java.sun.com/j2se/1.4.2/docs/...#toString(int)
Then you can use the String function "charAt".
Java Syntax (Toggle Plain Text)
int i; String myNumberAsString; 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)
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);
Last edited by cgeier; Oct 21st, 2009 at 9:57 pm.
•
•
Join Date: Sep 2008
Posts: 1,625
Reputation:
Solved Threads: 205
0
#8 Oct 22nd, 2009
•
•
•
•
Just modulus (%) by ten. That will give you the last digit.
Java Syntax (Toggle Plain Text)
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.
Last edited by BestJewSinceJC; Oct 22nd, 2009 at 1:46 pm.
Out.
•
•
Join Date: Sep 2008
Posts: 1,625
Reputation:
Solved Threads: 205
0
#10 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).

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.
![]() |
Similar Threads
- Payroll Help (Python)
- Please help me again? (Pascal and Delphi)
- ".class" expected error (Java)
- Maths problem (C#)
- Strings help (C#)
- Targeting a number within a set of integers (C)
- QuickSort Still Not Working?? (C)
- Problem Passing a Class Object to a Queue (C++)
- JAVA help (JSP)
Other Threads in the Java Forum
- Previous Thread: java program help/advice
- Next Thread: Help me
| Thread Tools | Search this Thread |
Tag cloud for int, integer, java
.net addball ajax apple applet appportability array automation bidirectional binary bold c++ c/c++ card char class classes code collision component consumer convert count counttheoccurenceofanintegerinthe10inputs design development digit draw ebook eclipse encryption error event firefox fractal fstream game glassfish google gui guidancer html hyper ifstream image infosec inheritance input int integer java javafx javascript jetbrains jsp julia key klocwork linux listbox loop method methods microsoft mysql netbeans newbie open-source oracle parsing php plazmic poi print problem programming project python ruby search security set smash socket software sort ssl string subclass sun support swing test textfields threads time tree web websites windows







