简体   繁体   中英

Getting arguments from the command line

I have wrote the following for scanning few lines of code as a string and parsing it to an integer. But within my main method, I am using p.letsReadIn to read in the file. Does anyone know how I can convert this to use command line argument instead for taking in the file? So instead of having the user change p.letsReadIn("ReadMuah.txt"); every time, they can just use command line just for reading in the file.

public void letsReadIn(String filename) throws FileNotFoundException {
        int x;
        Scanner scan = new Scanner(new File(filename));

        StringTokenizer st;
        String nextLine = scan.nextLine();

        st = new StringTokenizer(nextLine);

        x = Integer.parseInt(st.nextToken());

        nextLine = scan.nextLine();

        st = new StringTokenizer(nextLine);

        y= Integer.parseInt(st.nextToken());

        nextLine = scan.nextLine();

        st = new StringTokenizer(nextLine);

        z= Integer.parseInt(st.nextToken());

        for (int i = 0; i < x; i++) {
            nextLine = scan.nextLine();
            st = new StringTokenizer(nextLine);
            listing.add(Integer.parseInt(st.nextToken()));
        }

        scan.close();
    }
    public static void main(String[] args) throws FileNotFoundException {

        BagItems p = new BagItems();
        p.letsReadIn("ReadMuah.txt");
        p.StartBagging();

    }

The String[] args parameter for the main() method are the command line arguments. If you put the filename on the command line, it'll be in that array.

It depends what you mean by "use command line argument instead for taking in the file"

If you mean taking the arguments themselves from the command line (literally) then replace the Scanner and Scanner.parseInt calls with stuff that iterates over the String[] , and parses using Integer.parseInt(String) calls. But that's going to give you a non-user-friendly command line syntax, so you may want to add some "options" or whatever

If you mean that you want to be able to specify the source of the input parameters from the command line, then try something like this:

InputStream in;
if (args.length > 0) {
    in = new FileInoputStream(args[0]);
} else {
    in = System.in;
} = System.in
....
Scanner scan = new Scanner(input);
// Read as before.

This (or something like it) will allow your application to read the input either from a named file, or from standard input. In the latter case, you could run it like this (on Linux)

$ java ... my.Program << EOF
1
2
...
EOF

You could use p.parser(args[0]); , of course you need to check if args is containing some strings. ie you have passed an argument, just do if (args.length > 0) for that.

Use first element from args[] instead of hard coded file name in code.

public static void main(String[] args) throws FileNotFoundException {
    BagItems p = new BagItems();
    if (args.length > 0)
    {
        p.parser(args[0]);
        p.StartBagging();
    }
    else
    {
        p.parser("hard-coded-file.txt");
        p.StartBagging();
    }
}

Now you can pass file name to program as command line and your code should pick that.

Please note that array index start from 0 by default in java, so, to access first element of array (command-line arguments array) you should use [0].

For some more tips please follow the link below.

how to accept values using command line argument in java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM