• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch !
First, do you understand how command line parameters are passed to the main class ?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.






 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that there's also a constructor allowing you to pass the coordinates (new Point(x, y)).
 
reply
    Bookmark Topic Watch Topic
  • New Topic