Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing arguments from command-line to Point(x,y)

 
Amos Lai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm a beginner. I'm figuring out 'how to passing arguments from command-line to Point(x,y)? Any experts know the answer?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch !
First, do you understand how command line parameters are passed to the main class ?
 
eric totte
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a java application is run as:

java App 1 2

If the main method is defined as follows:

public static void main(String args[])

args[0] will contain the value "1"
args[1] will contain the value "2"

In your case, you would have to convert the String values to int before using them to initialize a Point object.






 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code won't compile because you're trying to store a String in a int.
Please DontBeACodeMill. Let him figure it out by giving some hints.
He'll learn a lot more from it.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
eric totte wrote:You can pass the arguments from command-line as follows:

No, you can't assign a String to an int

edit Did I really have that page open more than 8 minutes?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's 39 chars / (8 * 60) sec = 0,08125 chars a second
 
Amos Lai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaahh~ i finally got it... Thanks, guys!!


import java.awt.Point;

class cvtArguments2Point {
public static void main(String[] arguments) {
int A = 0;
int B = 0;

if (arguments.length > 0)
A = Integer.parseInt(arguments[0]);
if (arguments.length > 1)
B = Integer.parseInt(arguments[1]);

Point zpoint = new Point();
zpoint.x = A;
zpoint.y = B;

System.out.println("Convert arguments to Point(x,y)");
System.out.println("Point X is " + zpoint.x + "\nPoint Y is " + zpoint.y);
}
}
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that there's also a constructor allowing you to pass the coordinates (new Point(x, y)).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic