简体   繁体   中英

what is args.length < 1 in java?

I am quite new to Java so this might be quite simple for others.

I'm making a text summarizer by following a tutorial and I came across a slight problem. When I run my program, it outputs "Usage: java Main input.txt keywords ...". So it follows the code "if(args.length < 1)" to do that. However, how do I change it so it reads the text file and carry on with the program.

generatesummary class works perfectly.

if(args.length < 1){
  System.out.println("Usage: java Main input.txt keywords ...");
  return;
}

String filePath = args[0];

if(filePath == null){
  filePath = "./input.txt";
}

String[] keywords = null; 
if(args.length < 2){
  keywords = new String[1];
  keywords[0] = "";
}
else{
  keywords = new String[args.length-1];
  for(int i=1; i<args.length; i++){
    keywords[i-1] = args[i];
  }
}

System.out.print("keywords:\t");
for(String keyword : keywords){
  System.out.print(keyword+"\t");
  System.out.println();
}

//String[] keywords = null; 
Generator generator = new Generator();

generator.loadFile(filePath);
generator.setKeywords(keywords);
generator.generateSignificantSentences();
System.out.println(generator.generateSummary());
generator.generateSummary();

} }

It is a simple check to see whether the user has given at least one argument when launching the application or not. args.length gives the length of the array that holds the command line arguments. To be able to run it, you need to provide a fileName as an argument - that's what the error is suggesting. So in a command line you would write something like java Main filename.txt for it to be able to pass the check.

Your main method is like main(String[] args) . Now, when you tried to run that class you typed following command I guess.

java classname

So in this case args will not contain anything. But if you type

java classname fielName

args will contain the file name and it will not go inside of the first if block. Because length og args is 1.

args.length provides the length of the arguments sent to the Java application. If args.length < 1, that means no argument was sent to it.

You need to send in the file name that you want your application to process. If you're running Java from the command line, you can just add the file name to the command, such as "java Main file.txt".

Also note that if you're using an IDE to do your development, such as Eclipse, you'll need to send in the argument (file name) in a way that is particular to that IDE. You should consult the help for that IDE, or post here.

Lastly, you can directly just code in the file name in your program, instead of having to send it in through the command line. Get rid of the if(...) statement checking args.length, and change filePath declaration to:

String filePath = "/path/to/filename.txt";

Good luck and best wishes on your Java learning endeavor!

I understand you are executing that code inside a main method. The main method has the next signature:

public static void main(String[] args){ ... }

This method is your applications entry point in java. Watching it's signature you can see that it receives an argument named args that is an array of String's. As the main method is the entry point, this arguments come from the place you are executing the application (terminal, IDE, etc). lenght is and attribute of java arrays, that stores the number of elements that array has. By executing if(args.length < 1) you are checking if you received less than one argument, because the code is expecting a filename as argument. Yo have to execute your code form a terminal as the second line says

System.out.println("Usage: java Main input.txt keywords ...");

For adding arguments in eclipse you have to put them into "Run > Run Configurations...> Arguments". This article explains it perfectly: cs.colostate.edu/helpdocs/eclipseCommLineArgs.html

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