• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collecting filenames from command line?  RSS feed

 
Jackson Blackwale
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A question asks:

Instead of prompting the user for filenames, collect them from the command line (input file first, output file second. If the user does not enter two arguments on the command line an appropriate error message should be displayed.

I have no clue how to do this :S

And basically the program asks for a filename, reads that file, and outputs certain things to an output file. I have the program done I just don't understand the command line part of the question.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same thing.
 
Jackson Blackwale
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a bit confused still. The question is worded like "instead of prompting the user for filenames, collect them from the command line. Then it says if the user does not enter two arguments on the command line to display an appropriate error message.

It makes it sound like prompting user for filename and collecting from the command line are two different things?
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java allows programs to pass and expect parameters to the application's entry point (the main() method). So you can have something like:



(where a b and c are the params that you pass to the application org.foo.bar.MyApp)

This ultimately makes a b and c (and other such params) be accessible in the MyApp.main() method which might look like:



This is one way of accepting input for the program. The other way of course is to prompt the user for such input at an appropriate time from within the application.

So I think what the problem statement is saying is that you are expected to pass in the file names as the parameters to the application, like above.


 
Jackson Blackwale
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Hmm I don't remember learning anything like this in class. What is the topic called?
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Command line arguments. Here's the official tutorial http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
 
Jackson Blackwale
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so basically I need to assign args[0] to the inputfile and args[1] to the output file.

For example

>Java TextProgramClass inputfile.txt outputfile.txt

args[0] = inputfile.txt
args[1] = outputfile.txt


What I am confused with is because I am using eclipse. I do not see any command line all I have is my console. How do I go about doing it in eclipse?
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I can rephrase it. you should have a part of your program that has something very similar to this:



The key here is the String array called 'args'. That String array is comprised of different Strings that the program 'gets' from whomever is running the program. When someone runs the program from the command line, they will type java, then the name of the program, and then they can enter in characters, and those characters, whether they be file names or any other relevant data that the program might need to know to run properly, those characters will be stored in the aforementioned String array called 'args'.

So, inside your program you can reference the String array 'args' and get that data to use as you need to. So, for instance, if someone had run the program and typed "java yourProgram firstFileName secondFileName" (quotes only for clarity, they don't actually type the quotes), inside of your driver class you could reference the file name by referencing the String array 'args' by doing something like:


The first String inside the array (at location '0') is now assigned to the String variable called 'filename', and given my previous example would contain the String: firstFileName and args[1] would contain the String: secondFileName. Each subsequent String (if there were more than two) would be the next String in the 'args' array.

You can then use the file name however you see fit within your program. You can actually skip this step and just reference the String inside args[0] by typing 'args[0]' every time you need to reference the first argument entered after the name of the program was typed on the command line, but I like to reassign it just so I can have the variable name associated with the String have a more descriptive meaning.

Note that you can enter in as many arguments as you like when running the program from the command line, so the String array 'args' could contain as many pieces of data as you need.
 
Jackson Blackwale
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey james, thanks. You must have just posted before I edited my last post :P

My problem is I am using eclipse, how can I do the command line thing? I have only ever used eclipse. Is there a way to work with command line in it? When I start my programs all I do is press that big green play button XD
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way I know how to do it is running the program from a terminal. If you are using Linux, you can just run the program from your terminal, if you are using a Mac, you should have a terminal too, and in Windows you just use the command prompt. Eclipse may have direct terminal interface, but I don't know of any. So you would just compile your program and test it out from whatever terminal interface your operating system works with.
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To compile your program, a lot of people make something called a makefile, which isn't too complicated and you should look into when you have second, but for your purposes you can probably just compile it yourself from the terminal using the command 'javac fileNameOfDriverClass'. You have to be in the directory containing the driver class file to do this. All other associated files should be in the same directory too. Then type the command 'java fileNameOfDriverClass filename1 filename2' (<---your args array Strings) to run it from the terminal. I'm kind of new to Java too, but I had to do this numerous times and that's how I did it.
 
Jackson Blackwale
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Goto my program to work through my mac terminal, working on the program now. Thanks!
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool beans. Glad to be able to help.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!