• Post Reply Bookmark Topic Watch Topic
  • New Topic

User input from cmd for array?  RSS feed

 
Jeff Winston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


The user would input on cmd:  java ProgramName 20 60 which would give me a point (20,60) but not the whole array to make the polygon.
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Assuming those two lines of code are in your main() method, they should work.  What happened when you tried it (assuming you actually did try it)?
 
Jeff Winston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Welcome to the Ranch!

Assuming those two lines of code are in your main() method, they should work.  What happened when you tried it (assuming you actually did try it)?


It works for a single number for x but not for multiple numbers
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Winston wrote:It works for a single number for x but not for multiple numbers

I don't understand what you mean by that.  Please give an example of how you ran the program from the command line, the arguments you provided, what you observed, and what you expected.  As to "what you observed", what did the program output? What was the code that made the program produce that output?  What were you expecting would be the output?
 
Jeff Winston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Jeff Winston wrote:It works for a single number for x but not for multiple numbers

I don't understand what you mean by that.  Please give an example of how you ran the program from the command line, the arguments you provided, what you observed, and what you expected.  As to "what you observed", what did the program output? What was the code that made the program produce that output?  What were you expecting would be the output?


If you run the program as java ProgramName 20 60, it is only going to produce the single point (x,y) as I defined in the first post that int x = Integer.parseInt(args[0]); refers to the first number input which is 20 which is our x value. args[1] refers to the second number input which is 60 and refers to int y. Therefore we would only get (20,60), a single point. But what if I wanted to do multiple x values and multiple y values so I could have a set of several (x,y) points?
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no rule that says you can only pass in two arguments to a Java program via the command line.  You can pass as many command line arguments as you need your program to process.  How many arguments were passed is reflected in the length of the args[] parameter of the main() method. It's up to you to write your program in such a way that it can figure out how many pairs of coordinates have been passed to the program from the command line and what each arg element means (whether is an X value or Y value). The user of your program should understand how the program interprets the command line arguments to make the appropriate ordering of the arguments that they provide.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!