|
|
|
|
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.
|
|

02-18-2008, 01:18 PM
|
|
Member
|
|
Join Date: Feb 2008
Posts: 1
|
|
|
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
|
|

03-03-2008, 07:39 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
|
|
In Java command-line arguments are passes to the application main method using an array of strings.
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,
int numericValue = Integer.parseInt(arguments[0]);
this line of code gives a numeric value. So in your application you can do this.
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.
|
|

06-14-2008, 04:23 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 22
|
|
|
Notice that in java the argument 0 is not the executable like others languages like C/C++.
Ferran
|
|

06-15-2008, 07:59 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
|
|
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. 
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
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