Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-18-2008, 01:18 PM
Member
 
Join Date: Feb 2008
Posts: 1
denisatandi is on a distinguished road
Command line argument
I'm new to Java, can someone please tell me how you can use a command line argument to print a message several times? Say if you wanted to print Hello world three times and you have to enter 3 for the message to print three times.
Thank you
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-03-2008, 07:39 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
In Java command-line arguments are passes to the application main method using an array of strings.

Code:
public static void main (String[] arguments) { // Body of the main method. }
If you want to use a numeric value, you have to get it from a string array. Something like this,

Code:
int numericValue = Integer.parseInt(arguments[0]);
this line of code gives a numeric value. So in your application you can do this.

Code:
class Test { public static void main(String[] arguments) { int numberOfTurns = Integer.parseInt(arguments[0]); while(0 < numberOfTurns) { System.out.println("Hello World"); numberOfTurns--; } } }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-14-2008, 04:23 AM
Member
 
Join Date: Jun 2008
Posts: 22
ferranb is on a distinguished road
Notice that in java the argument 0 is not the executable like others languages like C/C++.

Ferran
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-15-2008, 07:59 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
I'm not clear what you are talking about. In my explanation, I'm not talking about anything related to executable commands. Basically over friend felt difficult with how to handle command line arguments in Java. May be the following code snippets can be helped. But I don't want to send this earlier because there is no response from thread starter.

Code:
class Test { public static void main(String[] arguments) { else if(arguments.length == 2){ String text = arguments[0]; int terms = Integer.parseInt(arguments[1]); for(int i = 0; i < terms; i++) { System.out.println(text); } } else { System.out.println("Invalid"); } } }
Here you need two command parameters to run the code.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java command line agouzoul New To Java 2 04-02-2008 01:12 PM
calling linux command line in java fangzhong New To Java 0 02-03-2008 05:24 PM
Printing command line arguments Java Tip Java Tips 0 12-03-2007 10:27 AM
Exporting from the command line o1121 Eclipse 1 08-09-2007 08:29 PM
Unable to execute command line command in java LordSM New To Java 1 08-08-2007 01:23 AM


All times are GMT +3. The time now is 09:13 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org