wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes passing arguments from command-line to Point(x,y) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "passing arguments from command-line to Point(x,y)" Watch "passing arguments from command-line to Point(x,y)" New topic
Author

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

Amos Lai
Greenhorn

Joined: Sep 21, 2010
Posts: 2
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Welcome to the ranch !
First, do you understand how command line parameters are passed to the main class ?


[My Blog]
All roads lead to JavaRanch
eric totte
Greenhorn

Joined: Aug 13, 2010
Posts: 10
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

Joined: Oct 25, 2008
Posts: 2700

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.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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?


luck, db
There are no new questions, but there may be new answers.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

That's 39 chars / (8 * 60) sec = 0,08125 chars a second
Amos Lai
Greenhorn

Joined: Sep 21, 2010
Posts: 2
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Note that there's also a constructor allowing you to pass the coordinates (new Point(x, y)).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: passing arguments from command-line to Point(x,y)
 
Similar Threads
how to read character from command line and store it string
array index out of bound problem
question about properties and command line arguements
Command line argument question
Q#7 Parikosh's test