2

I know that when you create your main method of a Class in Java you create it as follows:

public static void main(String[] args)

I also know that the array of Strings named args it's used to enter command-line parameters in the main method but I develop my applications on Eclipse and I never had used it because I always have entered my values by Scanner.

It is a bad practise to use Scanner instead of command-line parameters? Has args array another purpose? When should I use args array?

Thanks in advance!

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167

3 Answers3

3

In reverse order:

When should I use args array?

When you are passing command-line parameters.

Has args array another purpose?

No. It's for passing command-line parameters.

Is it a bad practise to use Scanner instead of command-line parameters?

It's not always possible to use a Scanner. A Scanner might read from STDIN, but in many UNIX systems it is prefered to read command input from STDIN and use command line arguments for program options (e.g. --help or -h).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Hi `Elliott`, I never had heard about `STDIN` (Thanks for add a new concept for me) and I saw this question: http://stackoverflow.com/questions/3385201/confused-about-stdin-stdout-and-stderr to search about it but I'm still a bit confused. It is a type of file? I also saw on `Wikipedia`: https://en.wikipedia.org/wiki/Standard_streams that `it's a stream data going into a program` (I think this should be the definition that you refer to) but I still have a doubt. Is it only the concept of a stream data (in general) or is it another method of enter parameters into my program? Thank you again! – Francisco Romero Sep 04 '15 at 22:17
  • In java, that is System.in which is where I assumed your hypothetical Scanner would be reading. – Elliott Frisch Sep 04 '15 at 22:42
  • Yes, I use my `Scanner` with `System.in`. Then, `STDIN` it's just a general concept to refer a `stream data going into a program` but not a method to get this goal, right? And it varies by the programming language, right? – Francisco Romero Sep 04 '15 at 22:45
  • No. It's the standard input mechanism on most operating systems. It might be a pipe or file, or user input. – Elliott Frisch Sep 04 '15 at 22:59
  • I'm sorry if I'm getting a wrong idea about it but... then `System.in` it's the same than `STDIN`? – Francisco Romero Sep 04 '15 at 23:08
  • `System.in` reads from `STDIN`. `System.out` writes to `STDOUT`. In Java. – Elliott Frisch Sep 05 '15 at 00:10
0

Scanner and the parameter for main are two completely different things. A scanner can be used to ask the user for some kind of action. The parameters for main are relevant when you want to launch the jar either from another application or via commandline. Cooked down: whether to use Scanner or parameters is rather a question of the design of the software, not coding-style.

0

It entirely depends on your circumstances. Ask yourself the question: "Where are the values coming from? What is the best way for my program to receive these values?"

If the answer is "a user", then prompting the user, thereby guiding the user to the values needed, is likely the best answer.

Any other answer means the values are likely coming from some automated process, which means that "prompting" is a bad choice. Now there are suddenly a lot of choices, again controlled by your circumstances:

  • Command-line arguments
  • Configuration file
  • Data file (e.g. CSV file)
  • Network communication (e.g. web service)
  • Database
  • ... and many more

Sometimes a combination is appropriate, e.g. a command-line argument to find the configuration file, which may include a directory to scan for data files.

You can even do both "prompting" and command-line arguments. If all needed values are supplied as arguments, use them, otherwise prompt for the missing values. Providing no arguments would then prompt for all values.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Correct me if I'm missing something but if I enter command-line arguments have to be at the same time I run the program? Or could it be whenever I want? – Francisco Romero Sep 04 '15 at 22:26
  • If the program is executed by a shell script (e.g. a .bat file on windows), then you write the command-line arguments in the script, not when you "run" the script. Same if executed by another program. This is the "not a user" scenario, because the user running the script is not supplying the values (unless the script supply *it's* arguments to your program). – Andreas Sep 04 '15 at 22:32
  • So, when I run my program on Eclipse at the same time a .bat file will be executed at the same time? (Of course I should configurate both programs to connect each other before running them, I guess) I'm sorry but I have some concepts in the air yet. And another question: the .bat file would be used if I know what parameters have to been used on my program? Something like a test with fixed values? – Francisco Romero Sep 04 '15 at 22:38
  • No, you generally don't run a shell script from Eclipse. If you want to supply command-line arguments when running from Eclipse, specify them on the Arguments tab of the Run Configuration. A .bat file *may* be used when running from a command prompt or windows shortcut on the desktop/menu. – Andreas Sep 04 '15 at 22:43
  • Ok now I think that I understand it. So in Eclipse add the arguments on the Arguments tab it's the same as command-line arguments on the Windows console, right? And what do you mean with desktop/menu? Thank you! – Francisco Romero Sep 04 '15 at 22:48