Results 1 to 6 of 6
  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default command line help

    If I had for example a class called Test and after compiling it, typing 'java Test 4 3' into the terminal would return 7 in the terminal.

    Could someone explain how to do the above, I know how to make the method etc and how to make it work with arguments in the code, but how would I do it so it works like the eg above.

    Thanks

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: command line help

    You said that you've already know how to create the methods how to handle the arguments passed from the console. Can you show us the code that you've written. And tell us which part that is not working.

  3. #3
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: command line help

    No, I know how to make it in a normal program without the console but I have no idea how to do it with the console.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: command line help

    To create an executable class you have to define an entry point method. In Java this method have the following signature:

    Java Code:
    public static void main(String[] args) {
    }
    The args array will contains the arguments you pass when you are executing the class using java command. For example if you pass characters 4 and 3 as the arguments then this arrays will have these two numbers in it.

  5. #5
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: command line help

    I'm a bit confused how I would write the rest.

    public static void main(String args[]){
    if (args.length() ==0)){System.out.println("No arguments");
    int total =0;
    for (int i =0; i < args.length(); i++){
    total = total + args[i];
    }
    System.out.print(total);
    }


    Would this be along the correct lines?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: command line help

    In your posting history, I see a whole lot of threads you started and didn't return to. From the links given to you in this one, I can see you didn't even bother to read the responses.

    Not the best way to get free help on a forum, that.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Command Line
    By dougie1809 in forum New To Java
    Replies: 7
    Last Post: 03-12-2012, 10:47 AM
  2. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  3. can i run line by line command in netbean?
    By choconlongxu in forum NetBeans
    Replies: 1
    Last Post: 07-19-2010, 08:41 PM
  4. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •