Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the last string in a list of command line options passed in via main(String[] args)? [duplicate]

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;
like image 630
MarkCoder Avatar asked Dec 14 '22 17:12

MarkCoder


1 Answers

You have to use

String inFile = args[args.length-1];
//array name    ^^^
//last index value   ^^^^^^^^^^^^^^
like image 169
Pavneet_Singh Avatar answered Dec 17 '22 22:12

Pavneet_Singh