Patricia Silva Patricia Silva - 6 months ago 12
Java Question

Dragon book compiler input

I'm executing the "Dragon book" front-end compiler, which expects a file input using

java main.Main < fileInput.txt


My question is: when I run args.length, the returned value is 0. Isn't fileInput.txt considered an argument? How could I catch it on code?

jso jso
Answer

No, you are not passing an argument to the program. Instead you are writing to standard input of the executed program (the content of fileInput.txt can be accessed via System.in).

Just in case, example reading:

Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()) { 
    System.out.println(scanner.nextLine());
}

Edit: Found a similar question now, see: Reading in from System.in - Java ;-)

Comments