I'm building a Java application that takes a varying number of command line options as input, with a file name consistently the last item. When I specify the argument index in the second line below (e.g. args[2]
, everything does of course work correctly when I know the index ahead of time, but I'm having trouble coming up with the correct syntax for accessing the final item in a String[]
when dealing with a file or even just a string as the input vs. an array of integers or something simpler for when that index number varies.
public static void main(String[] args) {
String inFile = args.length-1;
You have to use
String inFile = args[args.length-1];
//array name ^^^
//last index value ^^^^^^^^^^^^^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With