# Extracting individual numbers from a long integer

• 02-18-2011, 04:13 AM
CurbYourEnthusiasm
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
• 02-18-2011, 04:31 AM
Junky
Mod and divide. Some food for thought.
Code:

System.out.println(9876 / 1000);
System.out.println(9876 % 100);

• 02-18-2011, 05:09 AM
CurbYourEnthusiasm
I must be able to call the individual numbers by the single digit in a tester class
• 02-18-2011, 05:18 AM
Junky
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.
• 02-18-2011, 05:54 AM
CurbYourEnthusiasm
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?
• 02-18-2011, 05:57 AM
Junky
I'd use a while loop.
Code:

while some value is greater than 0 {
mod it
divide it
}

• 02-18-2011, 04:53 PM
CurbYourEnthusiasm
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