Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

passing in a file from the command line

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to pass in a text file from the command line to a class that reads it in, processes and counts its tokens then prints it out to a .dat file in my output directory. how do I connect what is entered in on the command line with the variable I have created in my class (it cannot be hard coded in my class)?

Here's my code:



[Edited to add UBB Code tags to make code reading easier - GB]

Please help
[ September 29, 2004: Message edited by: Gregg Bolinger ]
 
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, April -

My first question would have to be, where is your main method? The command-line arguments come into the program via main. Do you have another, driver program that invokes this class and its methods? Also, what error are you seeing? Compile-time, run-time, logical?

With more info, maybe I (or another visitor) can help.

Thanks,

Jeff
[ September 29, 2004: Message edited by: Jeff Bosch ]
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Just out of curiosity, why are these variables public instead of private, like the other variables? This would expose your variables to outside modification.
 
April Benzine
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those were just the specs of the project's class diagram
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May I ask what this project is for?
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't get either file to compile as is. I had 15 compiler errors with the AnalyzeFile and 21 with the driver program.

Is some code missing? Perhaps your import statements? Custom packages or classes? (The classes TokenCount and PropertiesManager aren't part of the standard API's that I can see.)
[ September 29, 2004: Message edited by: Jeff Bosch ]
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In your other post you write:

But why == 2 ?
Coming from c/c++?
args[0] is the first parameter in java, not the program-name.
One file argument leads to (args.length == 1).
[ September 29, 2004: Message edited by: Stefan Wagner ]
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
get data from command.

Run java program
java Test

Output
nothing

Run java program
java Test test1 test2 test3

Output
test1
test2
test3

 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, you should use Property file , it more flexible than current solution.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think so.
A parameter is immediately typed at the commandline, and if you prefer mice, you may move a file from the explorer to your well-defined jar, if you want your program to process that filename in some way (open the file, for example).
A properties-file needs to be found, edited, saved ..., you need backups to process older commands, while the commandline can have a history of thousends of calls...
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic