Thread: Extracting individual numbers from a long integer

1. Member
Join Date
Feb 2011
Posts
4
Rep Power
0

Extracting individual numbers from a long integer

I cannot figure out how to extract the individual number from an integer. For example 12546 how can I just get the "1" and then the "2". I would greatly appreciate any help I can get

2. Mod and divide. Some food for thought.
Java Code:
```System.out.println(9876 / 1000);
System.out.println(9876 % 100);```

3. Member
Join Date
Feb 2011
Posts
4
Rep Power
0
I must be able to call the individual numbers by the single digit in a tester class

4. and???

Did you look at my code? It wasn't the answer to your problem but merely to prompt you to think about it. Did you try mod and divide with other numbers such as 10,000 1,000,000 100,000 etc and see if any of them might help you.

5. Member
Join Date
Feb 2011
Posts
4
Rep Power
0
yes I have figured out how to extract the individual numbers, thank you. I do not understand the concept of returning a long integer broken into individual number to a single method. Would I use a for or if statement?

6. I'd use a while loop.
Java Code:
```while some value is greater than 0 {
mod it
divide it
}```

7. Member
Join Date
Feb 2011
Posts
4
Rep Power
0
public class DigitExtractor {
private static int intNumber;
public DigitExtractor(int anInteger)
{

intNumber = anInteger;
String number = String.valueOf(intNumber);
char charNumber = number.charAt(4);
int x = number.charAt(3);
int x1 = number.charAt(2);
int x2 = number.charAt(1);
int x3 = number.charAt(0);

}

public static String nextDigit()
{

I dont understand if I return one number is only gives me that number. Even if I use the while Loop it will not let me return more then one number. I am complelty stuck

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•