This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Command-Line arguments  RSS feed

 
Surya Indukuri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to take arguments from the command line.
once the user enters java test,
I need to display a message "please enter to and from date as yyyy/mm/dd"
when the user enters the data I need to accept it may be validate in my main args.

How can I di it?

ThankYou
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with the Scanner class.
 
Surya Indukuri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You. I will try but, I am not sure how to use it.
I tried the following and it worked but, it is a lot of code.

System.out.println("Please enter From date (yyyy/mm/dd)");
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
try {
dateFrom = br1.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read from date!");
System.exit(1);
}
 
Tiku Popat
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use StringTokenizer if you are not comfortable with Scanner. But I guess they both are some what similar.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Surya Indukuri:
...I tried the following and it worked but, it is a lot of code...

That's a benefit of Scanner. You can replace all of that IO code with the following...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that Scanner is only available as of Java 1.5. If you are using an earlier version of Java then streams are the only way of getting input from a text-based interface.

Also, I would like to clarify that these are not command-line arguments. A command-line argument is specified in the command that you use to run the program. For example:

Here "arg1" and "arg2" are command-line arguments and are passed to your program as the String[] parameter to main(). If you really need to use command-line arguments, then you need to back up a little.

Layne
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

System.out.println("Please enter From date (yyyy/mm/dd)");
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
try
{
dateFrom = br1.readLine();
}
catch (IOException ioe)
{
System.out.println("IO error trying to read from date!");
System.exit(1);
}


I agree that's a fussy little chunk of code. Which is a good enough reason to put it in its own method or better yet in its own class. With Buffered Reader or Scanner either one see if this would be useful:

You'd want to think about those exceptions. Maybe catch IO exceptions and throw PromptException so the calling routine can decide what to do.

If you're going to make a lot of console programs, you might want the prompt method to validate the data. In another language that was all console stuff I had prompt for number, date, string, a choice from a list, etc. The prompt routine looped the display/read part until it got a good value or a special token for quit. That could be a real fun exercise if you need it.
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here "arg1" and "arg2" are command-line arguments and are passed to your program as the String[] parameter to main(). If you really need to use command-line arguments, then you need to back up a little.

Layne


I concur with Layne, command line arguments truely means that when you run your program from the command line say "java RunTest" you must include the arguments here.So it would be like, "java RunTest arg1 arg2 arg2" and so on.Then notice how in your main method it has the parameters (String[] args) . To access arg 1 its args[0]...arg2 is args[1] and so on. Here is a little tutorial from sun if you did not follow.Command Line Tutorial

hope it helps
-Luc
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!