Results 1 to 4 of 4
Like Tree3Likes
  • 1 Post By Norm
  • 1 Post By jfabian
  • 1 Post By cselic

Thread: Digit Extractor

  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Post Digit Extractor

    Hi All! I am going to write a program that reads an integer and breaks it into a sequence of individual digits in reverse order, there will be no more than five digits and they are not negative, I started coding,
    DigitExtractor class:
    Java Code:
    
    public class DigitExtractor {
    	/**
    	 * Constructs a digit extractor that gets the digits
    	 *  of an integer in reverse order.
    	 *  @param anInteger the integer to break up into digits
    	 */
    	public DigitExtractor(int anInteger)
    	{
    		String number = String.valueOf(anInteger);
    		char a=number.charAt(4);
    		char b = number.charAt(3);
    		char c = number.charAt(2);
    		char d = number.charAt(1);
    		char e = number.charAt(0);
    		
    	}
    	/**
    	 * Returns the next digit to be extracted.
    	 * @return the next digit
    	 */
    	public int nextDigit()
    	{
    		
    	}
    
    }
    the question is I don't know what to do with "public int nextDigit()" part...Can you advice me guys what kind of commands I should use here?Thanks in advance!

    There is also DigitPrinter class:
    Java Code:
    public class DigitPrinter {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		DigitExtractor myExtractor= new DigitExtractor(35791);
    		System.out.println(myExtractor.nextDigit());
                    System.out.println(myExtractor.nextDigit());
                    System.out.println(myExtractor.nextDigit());
                    System.out.println(myExtractor.nextDigit());
                    System.out.println(myExtractor.nextDigit());
    
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Digit Extractor

    I don't know what to do with "public int nextDigit()"
    The doc for it says: Returns the next digit to be extracted.
    That implies there is an order for the digits with a first and last and current.
    What/where are the digits? Which is the first and which the last?
    How do you keep track of where you are when extracting digits?

    The "storing" of individual digits will be more workable if you use an array instead of 5 variables.
    Why not wait to extract the digits until they are requested?
    Last edited by Norm; 06-30-2012 at 10:47 PM.
    FOX427 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jfabian is offline Member
    Join Date
    Jun 2012
    Posts
    22
    Rep Power
    0

    Default Re: Digit Extractor

    Another option is to use an array declared as a global variable and then just iterate the array with a loop in nextDigit().
    FOX427 likes this.

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Digit Extractor

    @FOX427: I think you should use a better idea, this is not good approach.

    For example, here you have:
    Java Code:
    public DigitExtractor(int anInteger)
        {
            String number = String.valueOf(anInteger);
    Well if anInteger is int let's work with int anInteger to the end instead of "converting" int anInteger to String number and work with strings.

    I think that better idea for this will be combination of div and mod.

    If we have for example number 175 what should we do:

    number = 175
    some loop {
    ask if our number is zero. If it's zero it's end of the loop
    1. 175 mod 10 = 5
    2. 5 is our first number
    3. 175 div 10 = 17
    1. 17 mod 10 = 7
    2. 7 is our second number
    3. 17 div 10 = 1
    1. 1 mod 10 = 1
    2. our third number is 1
    3. 1 div 10 = 0
    }
    because it's zero it's end of the loop
    Last edited by cselic; 07-05-2012 at 01:59 AM.
    FOX427 likes this.

Similar Threads

  1. Want Extractor icon in eclipse
    By alisha5 in forum Eclipse
    Replies: 1
    Last Post: 12-02-2011, 02:44 PM
  2. Summing the digit
    By gozuhair in forum New To Java
    Replies: 13
    Last Post: 07-18-2011, 07:09 AM
  3. Replies: 0
    Last Post: 01-23-2011, 12:37 AM
  4. 4 digit restriction
    By GTM in forum Java Applets
    Replies: 3
    Last Post: 12-28-2010, 02:26 AM
  5. digit spacing
    By puk284 in forum New To Java
    Replies: 3
    Last Post: 06-09-2009, 02:49 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
  •