This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
Hello all ... I'm passing two parameters to my "main" method.
a path, and a "name" e.g. java APPC_LU62.Runtime.LU62XnsCvr C:\APPC_LU62\CA_DMV ABDSI000
1) Do the passed parameters have to be within quotes ? i.e. "C:\APPC_LU62\CA_DMV ABDSI000"
2) Does the "path" parameter require the "double" backslash ?
3) What's a good technique for determining if the parameters have been passed?
e.g. testing the length of the argument string passed ?
copying them into Strings like that seems a waste. param1 doesn't tell me any more or less than args. I could see copying it into a better-named variable like 'path_to_executable' and 'executable_name'.
I guess there are two things you need to check:
1) did they pass in the correct number of params. This is quick and easy, although not TECHNICALLY necessary. But if you know you need to have two, you can immediately deal with it if they only give you 1. Plus, that lets you avoid checking to be sure args is not null.
2) For each parameter you get, you should validate it in whatever way is appropriate for you. Does the path need to be an existing directory? Can you create it if it doesn't exist? Does the file need to be there already? etc.
I am honestly not sure if you need the double-backslashes in the string when you input it on the command line, although my GUESS is no, you don't. My suggestion would be to try it and see what works, which is what I would have to do to get you a definitive answer.
Guy Rich wrote:2) Does the "path" parameter require the "double" backslash ?
You would only use the double backslash when the context requires you to escape backslash characters. By far the commonest context which requires that is the Java string literal. For example the Java string literal "a\\b" consists of three characters, namely a and \ and b. There's a couple of other obscure places in the Java world which require that escaping, but elsewhere it isn't necessary.
In this case you're asking about whether the Windows command line requires you to escape backslashes. And no, it doesn't.