• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Sea Poked
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
Daryl Cofer
Greenhorn
Posts: 19
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking how to run your application from the command prompt?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • 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: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Corrected argument to parameter in previous post.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!