Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default 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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

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

  3. #3
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    I must be able to call the individual numbers by the single digit in a tester class

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    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. #5
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    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. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

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

  7. #7
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    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

Similar Threads

  1. Letter L or l to long integer?
    By Jagdeesh123 in forum New To Java
    Replies: 1
    Last Post: 12-09-2010, 08:05 AM
  2. Please Review My Code (Long Integer Addition)
    By Saradus in forum New To Java
    Replies: 12
    Last Post: 07-05-2009, 01:01 PM
  3. extracting bits from big numbers
    By ankitmcgill in forum New To Java
    Replies: 6
    Last Post: 05-05-2009, 04:36 AM
  4. Calculating Individual Numbers
    By TheKnight in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 12:51 AM
  5. Calculate sum of long integer!
    By Julingo in forum New To Java
    Replies: 2
    Last Post: 09-09-2008, 11:50 PM

Posting Permissions

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