Sprint
Sprint

Reputation: 179

When should we use command line arguments?

I found codes getting input as command line arguments.I am using Eclipse but the example code in Algorithms 4th Edition accepts args[0] and so on.I knew we can input argument in Eclipse.
Edit starts
In Eclipse,I need to go to the Run Configuration in order to enter the argument when there is a line of code like this int T = Integer.parseInt(args[0]);
But it's obvious that if we write the codeint T = scanner.nextInt();
I don't even need to go to Run Configurationto enter the Program Arguments and all I need is just click the run button and input the value in the console of Eclipse.
Edit ends.
So,My question is:

1)What is the function(s) for using command line arguments when we can solve the problem using Scanner class?

Thanks for any explanations!

Upvotes: 1

Views: 853

Answers (2)

Andreas
Andreas

Reputation: 159086

When you compile your code, you execute javac MyClass.java
When you run the code, you execute java MyClass.

Well, MyClass.java is a command line argument to the javac command, and MyClass is a command line argument to the java command.

Do you think those commands would be better if they instead stopped and asked for the values they need? Especially considering there are a lot of options that can be set, so should they ask for each option, one at a time, so you'd have to press Enter 20+ times before the compilation started?

That is just 2 examples for command-line arguments, and how useful they can be.

Let's use another example. We want to create a Zip file, which at its simplest need a name for the Zip file, and a list of file names to add to the Zip file.

Using Scanner, that might look like this:

java CreateZip
Enter name of Zip file: foo.zip
Enter name of file to include, blank when done: Hello.txt
Enter name of file to include, blank when done: Yeehaa.txt
Enter name of file to include, blank when done: bar.doc
Enter name of file to include, blank when done: baz.png
Enter name of file to include, blank when done:
Zip file created

Or you could use command-line arguments:

java CreateZip foo.zip Hello.txt Yeehaa.txt bar.doc baz.png
Zip file created

Which approach is better? What if you want to do that from a script? How can the script answer questions?

Upvotes: 1

Belal Khan
Belal Khan

Reputation: 2119

That is just an another way of doing things. Sometimes command line arguments are useful for quickly checking how your program responds with different inputs. I guess you just started your programming journey and that is why you are asking question like this.

And let me tell you there is a difference between argument and input. Scanner class helps you to take input from the console whereas command line arguments are passed as an argument to your main function.

Here are some advantages of using command line arguments.

  1. You can pass any number of arguments and you do not need to define variables for them.
  2. The next benefit is you can pass any data type with command line and then you can code your functions accordingly.

And the final answer for you, don't think about why this when we already have this. It is programming and you should learn as much as you can. You will find 1000s ways of doing the same thing. So enjoy learning.

Upvotes: 1

Related Questions