Results 1 to 3 of 3
  1. #1
    jeffrey12 is offline Member
    Join Date
    Apr 2017
    Posts
    3
    Rep Power
    0

    Default User input from cmd for array?

    I wanted to create a polygon with the points being defined by the user via cmd.

    Using an example I found on the internet:

    int [ ] x = {20, 35, 50, 65, 80, 95};
    int [ ] y = {60, 105, 105, 110, 95, 95};
    g.drawPolygon(x, y, 6);

    Let's say I wanted the user to input x and ys so I can draw the polygon. How would I do it?

    I was thinking something like

    int x = Integer.parseInt(args[0]);
    int y = integer.parseInt(args[1]);

    The user would input on cmd: java ProgramName 20 60 which would give me only a point (20,60) but not the whole array to make the polygon unfortunately.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: User input from cmd for array?

    Check out the Scanner class. The nextInt() method may be of use.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: User input from cmd for array?

    not the whole array
    I don't know what the limit for the number of args is. Maybe you can enter hundreds of them. Try a test to see how large an array of args you can pass to main.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. 2d array input user for gui
    By rahilgada in forum New To Java
    Replies: 2
    Last Post: 03-25-2014, 11:39 AM
  2. How to print array from user input?
    By sims34 in forum New To Java
    Replies: 1
    Last Post: 01-29-2013, 04:46 AM
  3. User input to Array Question
    By LukasHopkins in forum New To Java
    Replies: 5
    Last Post: 08-14-2012, 01:12 PM
  4. Pass User Input into an Array
    By Rahim2312 in forum New To Java
    Replies: 2
    Last Post: 05-14-2012, 05:45 PM
  5. user input array
    By localhost in forum New To Java
    Replies: 5
    Last Post: 12-30-2010, 04:00 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
  •