Results 1 to 4 of 4
Thread: Digit Extractor
- 06-30-2012, 10:35 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
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:
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!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() { } }
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()); } }
- 06-30-2012, 10:44 PM #2
Re: Digit Extractor
The doc for it says: Returns the next digit to be extracted.I don't know what to do with "public int nextDigit()"
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.
If you don't understand my response, don't ignore it, ask a question.
- 07-01-2012, 10:27 AM #3
Member
- Join Date
- Jun 2012
- Posts
- 22
- Rep Power
- 0
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().
- 07-05-2012, 01:57 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Re: Digit Extractor
@FOX427: I think you should use a better idea, this is not good approach.
For example, here you have:
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.Java Code:public DigitExtractor(int anInteger) { String number = String.valueOf(anInteger);
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 loopLast edited by cselic; 07-05-2012 at 01:59 AM.
Similar Threads
-
Want Extractor icon in eclipse
By alisha5 in forum EclipseReplies: 1Last Post: 12-02-2011, 02:44 PM -
Summing the digit
By gozuhair in forum New To JavaReplies: 13Last Post: 07-18-2011, 07:09 AM -
User Enters 4-Digit Integer, Console Returns 1 Digit Per Line
By STANGMMX in forum New To JavaReplies: 0Last Post: 01-23-2011, 12:37 AM -
4 digit restriction
By GTM in forum Java AppletsReplies: 3Last Post: 12-28-2010, 02:26 AM -
digit spacing
By puk284 in forum New To JavaReplies: 3Last Post: 06-09-2009, 02:49 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks