-2
public class TFIDF {

   public static void main (String args[]) {

      if (args.length < 2)  {
         System.out.println("Use Two Files!");
      }
}

Hi, I have this small section of code from a larger program which allows me to input txt files through the command line using args, but it only allows for two txt files to be input, how would I make it so that it allows for one or two txt files.

Peter Toole
  • 93
  • 2
  • 9
  • The question is a bit uncleare (at least for me) . `args[]` contains all given command line arguments delimited by a space which means when you call something like `java Main blaBla.txt test.csv` then `args[0]` contains the **String** `blaBla.txt` and `args[1]` the String `test.csv`. So you can check wheather you have enough arguments or not by simply checking the length of `args` (what you already did in your example). What exactly do you want to know? can you a bit more specific? – L.Spillner Apr 06 '18 at 13:13
  • 1
    It sounds like you're asking how to change `if (args.length < 2)` to some other condition. Is that it? – khelwood Apr 06 '18 at 13:14
  • @khelwood yeah that is what I want, that condition only allows for two txt files to be uses through the command line, so I want it to allow one or two text files, hope this is clearer :) – Peter Toole Apr 06 '18 at 13:20
  • https://stackoverflow.com/q/367706/1531971 –  Apr 06 '18 at 14:00

3 Answers3

0

If you want the command lin to give one or two file, it means you give one or two arguments. So args.length will be equal to 1 or 2. Just make a if corresponding to this.

if(args.length > 0 && args.length <= 2) {
    //Number of arguments correct
} else {
    //number of argument wrong
}
vincrichaud
  • 2,218
  • 17
  • 34
0

This is hardly a code question, its more of a logic question.

You are saying that one or two files is fine, but anything more is not, so use this inequality:

if (args.length > 2)  {
    System.out.println("Use One or Two Files!");
    System.exit(1);
}

// getting the arguments
String pathToFile1 = null;
if(args.length >= 1)
    pathToFile1  = args[0];

String pathToFile2 = null;
if(args.length >= 2)
    pathToFile2  = args[1];

// then just be aware that a path could be null

Aside from just checking that the arguments exist, you should probably do checks on the paths that were input and check that the files exist and that they are actually files and not directories.

xtratic
  • 4,600
  • 2
  • 14
  • 32
  • I tried that but it didn't work, I received this error from cmd "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at TFIDF.main(TFIDF.java:21)", this still only words for 2 files. For test purposes I am presuming the files exists so its easier – Peter Toole Apr 06 '18 at 13:24
  • please update the question with the command you are using to run and also add more code, including line 21 of TFIDF where the exception is occurring. – xtratic Apr 06 '18 at 13:29
  • @PeterToole also if the arguments are incorrect, be sure to exit the program after writing the error message. – xtratic Apr 06 '18 at 13:47
  • 1
    Don't access `args[1]` when `args.length` is only `1`. Indices start at 0. – KYL3R Apr 06 '18 at 13:52
0

args represents each argument in string array from command line, so as others mentioned in case java Main blaBla.txt test.csv you got following in the program:

  • args[0] blaBla.txt (first argument)
  • args[1]=test.csv (second argument)

So you can make easy check, for explanation:

if (args.length == 0 || args.length > 2) {
 System.out.println("please provide one or two files");
} else if (args.length == 1) {
 // got only 1 file
 // file1 = args[0]
} else {
 // args.length == 2 got 2 files
 // file1 = args[0]
 // file2 = args[1]
}

Which can be optimized like following:

if (args.length == 1) {
 //got one file
} else if (args.length == 2) {
 //got two files
} else {
 //invalid arguments
}

to avoid redundant assign file1 in case of arguments are correct can do eg. this

if (args.length > 2 || args.length == 0) {
 //none or more arguments then supported, some fail
 System.out.println("Use One or Two Files!");
 System.exit(1);
}

if (args.length == 1) {
 //got one file or two files
 //assign first

}

if (args.length == 2) {
 //got two files
 //also got second
}
xxxvodnikxxx
  • 1,270
  • 2
  • 18
  • 37