• 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

Extending class and entering arguments from command prompt (i.e. java SpecialClass Hello)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class 'human' and a class 'kid' that extends 'human'. I have generic ints (age/height/weight) and my constructor is



I am not 100% sure why I need to do that, but I want to start a certain file (java Kid) and input age/weight/height as I launch it (java Kid 10 120 50).

Help is appreciated
 
Greenhorn
Posts: 19
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you asking how to run your application from the command prompt?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so what exactly is your question?

Have you noticed the args argument that is passed to the main method? It contains the arguments that you type on the command line after java Kid. You'll want to parse those arguments into integers (using Integer.parseInt()) and then use the resulting values to create a Kid object.

See Command-Line Arguments
 
Sea Poked
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying!

1. I know how to run it from the command line

2. I am asking how to read input from the command line without asking for it (without saying "please input x and y"). Like 'java Human John 28' (name-age)

[edit] Also: should I write String[] args or String args[]? I put the argument brackets after the 'args' key word.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the link that I posted above, it explains exactly how to do that.

Always write String[] args. The syntax "String args[]" also exists, but it was included in Java only to make Java look familiar to C and C++ programmers. The "[]" means "array". It's much more logical to put it behind "String", since the "[]" says something about the type of the variable.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • The type of the object pointed to by “args” is an array of Strings.
  • It is ∴ good practice to declare it as an array of Strings.
  • Its type is ∴ String[].
  • You ∴ write the type before the identifier.
  • You ∴ write String[] args in preference to String args[] as you find in some old books.
  • It says in the Java Language Specification that you can write String... args instead.
    If you have an anonymous class which uses any of the parameters (in this case, args), you must declare that argumentparameter as final:
    public static void main(final String... args) or
    public static void main(final String[] args)

    You would expect the user to enter any details at the command line, and as you have been told, anything following Kid becomes an element in the args array. You can read about it in the Java Tutorials. Remember that numbers, etc., have to be parsed with parsing methods like this.
    The user needs to know to enter the arguments, and that goes in the documentation comments. The convention for dealing with command‑line arguments is like this:-
     
    Campbell Ritchie
    Marshal
    Posts: 79178
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Corrected argument to parameter in previous post.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic