• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing Parameters to main

 
Ranch Hand
Posts: 33
Eclipse IDE Redhat Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

Thanks

Guy
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It took me about 1 minute to write some code that let me test what you are asking about.

1) if a single parameter has a space in it, it needs to be in quotes. The space is the default (only?) character used to determine what the individual params are.

2) not inherently, no. Although, depending on what you want to DO with it, it might.

3) length is a good start. You may want to do additional verification...i.e. if one should be a number, you'll probably want to do some kind of string-to-integer conversion.

 
Guy Rich
Ranch Hand
Posts: 33
Eclipse IDE Redhat Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you again Fred ... So I can copy the arguments into a couple of string variables then check the length of each one ?

e.g. String parm0 = arg[0] ;
String parm1 = arg[1] ;

if (parm0.length < 3)
{ ...send_error_mesg }

if (parm1.length < 2)
{ ...send_error_mesg }

The path parameter will be used to locate files at runtime ...

e.g. StringBuffer full_path new StringBuffer() ;

full_path.append(parm0) ;
full_path.append("\\") ;
full_path.append("file_name.txt") ;

Comments and/or suggestions most welcome

Thanks

Guy
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
copying them into Strings like that seems a waste. param1 doesn't tell me any more or less than args[0]. 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[1] 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.
 
Ranch Hand
Posts: 54
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Guy Rich wrote:2) Does the "path" parameter require the "double" backslash ?



My recommendation......use the "/" and don't worry about it. Using the forward slash (like in linux) allows Java to determine what the OS needs (Java will convert to a "\" if the os requires it).
 
Guy Rich
Ranch Hand
Posts: 33
Eclipse IDE Redhat Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred & Jared Thank you both very much.

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic