Results 1 to 3 of 3
Thread: code for five digits
- 02-13-2011, 12:22 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 1
- Rep Power
- 0
- 02-13-2011, 02:15 AM #2
Make a simple buffer or queue? If you made some kind of buffer, you could make it only hold 5 digits, and as more digits are added, the initial ones get popped off. If the size of the buffer was smaller than 5, you could prompt for more input!
- 02-13-2011, 02:25 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Note that expressions like "the last five numbers" are most naturally interpreted as statements about Strings not numeric quantities. Your task would not be noticably different if colour words, people's names, etc rather than numerals were being input.
I would, therefore, read the input as a String (ie not use Scanner's ability to return an int) and then use the String methods to get the length of the string and, if necessary, obtain a substring. Finally I would use Integer.parseInt to obtain the number and check that the String is a well formed integer numeral.
(This assumes that you are after an integer value - if you are after something else like a floating point value or a whole number etc the last step will change.)
Similar Threads
-
Value should be 7 or 8 digits .If 8 digits, the last should be a character
By renu in forum New To JavaReplies: 1Last Post: 01-19-2011, 09:23 PM -
reversing digits
By gandalf5166 in forum New To JavaReplies: 3Last Post: 03-06-2010, 04:34 PM -
Digits of an integer.
By Allgorythm in forum New To JavaReplies: 8Last Post: 01-01-2010, 02:34 AM -
split() by digits
By RobertF in forum New To JavaReplies: 2Last Post: 03-12-2009, 02:16 AM -
sum of digits depreciation
By jleas in forum New To JavaReplies: 13Last Post: 11-09-2008, 01:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks