DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2005

    Question getting user input & seperating into integers

    What I am trying to do is get a list of numbers from the user, the numbers must be in the 1 - 9 range so they will all be single digits. I do not know ahead of time how many numbers there will be, except that it will be at least 1 and no more than 4.

    I was using the following code to get them one at a time:
    		Scanner in = new Scanner(System.in);
    		System.out.println("Enter a number: ");
    		int yourInt = in.nextInt();
    I could put this in a loop until they enter something out of range but I don't really like that idea. I would to have something like this:

    Enter some numbers: 2 5 1
    and end up with an int array x such that
    x[0] = 2;
    x[1] = 5;
    x[2] = 1;

    And I actually don't care about the order.

    It is just a simple console based application for my own use so at this point I am not worrying about the exception handling.

    Any thoughts or ideas?

  2. #2
    Join Date
    Nov 2004
    If the numbers are all the input you need then why not supply the numbers at application
    startup like:

    java MyApp 2 1 5

    and pick them up in the
    public static void main(String [] args) {
      // check args.length, 
      // if ok convert each String element to an int, 
      // bail out at first NumberFormatException
    If not, then there is nothing wrong with an input loop that stops at first blank entry,
    and re-prompts at invalid numerics. This would involve NumberFormatException
    handling though.
    eschew obfuscation

Similar Threads

  1. Replies: 0
    Last Post: 02-05-2002, 03:11 PM
  2. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 11:45 AM
  3. Check if an application is waiting for user input
    By Gerardo Gomez in forum VB Classic
    Replies: 0
    Last Post: 06-23-2000, 11:00 AM
  4. Creating a user input search
    By oSSiE in forum ASP.NET
    Replies: 0
    Last Post: 05-01-2000, 06:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.