• 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

Order of operations when reading command line args?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, newbie here. First post, learning Java, etc. I've made it through most of the Head First book and also a professor-authored book for the class I'm taking. I'm trying to accomplish something for an assignment which requires getting command line args from a text file, which I can do, but I'm finding that later references in my main method to system input seem to be getting processed before I think they should. In short, things are happening in a confusing order. I've simplified what I'm working on into a test code to isolate the issue. Please see the files below. I am executing:

java argstest.java<args.txt

I would expect the output to be 1 1 1 10 10 10. But that actual output is 1 1 1 10. I would expect the for loop to go through all iterations of arg BEFORE the do/while loop beings executing, but this doesn't seem to be the case. Is there an explanation for this, and is there a way I can change this behavior?

File argstest.java:

File args.txt:
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using < on the command line does not make the contents of args.txt get passed as command line parameters, it just puts them onto the standard input stream which is a different thing.
So, as there are no command line arguments, args.length is zero and so lines 11 - 15 are not executed.
The flow therefore does reach the do/while loop and reads in the values from the file. However, as soon as it reaches the first 10, i will be set to 10 and so the loop will exit.

Try running your program as
java argstest 1 1 1 10 10 10
and you will see something closer to what you were expecting. Note however that once the loop in lines 11 - 14 finishes, line 15 will execute and the program will exit.
 
Matt Hoover
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:Using < on the command line does not make the contents of args.txt get passed as command line parameters, it just puts them onto the standard input stream which is a different thing.
So, as there are no command line arguments, args.length is zero and so lines 11 - 15 are not executed.
The flow therefore does reach the do/while loop and reads in the values from the file. However, as soon as it reaches the first 10, i will be set to 10 and so the loop will exit.


Thanks, I just sort of figured out the same thing myself. My real question is bigger and I am still trying to find out the correct way to word it.

If I am asked to "use a filename as a command line argument," would that mean:

java program<file.txt
OR
java program file.txt

In the first case I would call it piped input, and the second case I would say the filename is being used as a command line argument. But just trying to make sure I understand the terminology. Thanks.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matt Hoover wrote:java program<file.txt
OR
java program file.txt

In the first case I would call it piped input, and the second case I would say the filename is being used as a command line argument. But just trying to make sure I understand the terminology. Thanks.


Your correct about the second case.
The first is redirection of standard input.
Piped input is slightly different in that you use the output of one program as input into a second program. You use the | symbol to do that.
A simple example
dir | sort
This would connect the standard output of the dir command to the standard input of the sort command and so give you a directory listing in alphabetical order.
reply
    Bookmark Topic Watch Topic
  • New Topic