Results 1 to 6 of 6
Thread: command line help
- 12-09-2012, 05:18 AM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
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
- 12-09-2012, 05:24 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
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.
Website: Learn Java by Examples
- 12-09-2012, 05:35 AM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
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.
- 12-09-2012, 05:41 AM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
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:
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.Java Code:public static void main(String[] args) { }Website: Learn Java by Examples
- 12-09-2012, 05:54 AM #5
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
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?
- 12-09-2012, 06:29 AM #6
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Command Line
By dougie1809 in forum New To JavaReplies: 7Last Post: 03-12-2012, 10:47 AM -
Open a URL and read it line by line (Works in Eclipse but not from Command Line)
By rosco544 in forum NetworkingReplies: 16Last Post: 09-17-2011, 02:41 AM -
can i run line by line command in netbean?
By choconlongxu in forum NetBeansReplies: 1Last Post: 07-19-2010, 08:41 PM -
Formatting java command line output - Multi line string
By dricco in forum New To JavaReplies: 2Last Post: 07-02-2010, 02:20 PM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks