Results 1 to 3 of 3
  1. #1
    TheAisBack is offline Member
    Join Date
    Feb 2011
    Rep Power

    Default code for five digits

    How do you restrict a java program to use just the last five numbers. For example if I type in 5678955. The program would only use 78955 and if I only type in 4 numbers the java program tells the user that you didn't type enough numbers try again.


  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Rep Power


    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!

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    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

  1. Replies: 1
    Last Post: 01-19-2011, 09:23 PM
  2. reversing digits
    By gandalf5166 in forum New To Java
    Replies: 3
    Last Post: 03-06-2010, 04:34 PM
  3. Digits of an integer.
    By Allgorythm in forum New To Java
    Replies: 8
    Last Post: 01-01-2010, 02:34 AM
  4. split() by digits
    By RobertF in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 02:16 AM
  5. sum of digits depreciation
    By jleas in forum New To Java
    Replies: 13
    Last Post: 11-09-2008, 01:37 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