简体   繁体   中英

Printing number of command line arguments of Java application

suppose the arguments from the command line: Hello this is a message

I want to print to the screen the number of the arguments, for one time only and to be printed at the top.

also how to print, "message" before "this" or even before any arguments , however the user is going to write "this" at index 0, and "message" can be at any index?

 for (int i=0; i < args.length; i++)
{


if(i= args.length) {System.out.println(i);} //this command says, 
 you cannot convert from int to boolean

System.out.println(args.length); // this command prints repeatedly 



if (args[i].startsWith("this"){System.out.println(args[i]);}

if (args[i].startsWith("message"){System.out.println(args[i]);}

} // end of for
if(i = args.length) //Here is the problem

By mistake you are using single '=' to compare i with args.length. You need to use '==' to compare i with args.length.

You need to do like below

if(i == args.length)

Your condition never true , It never goes inside.

See the reason.

 for (int i=0; i < args.length; i++)  //  looping with less  than.
{


if(i== args.length) {System.out.println(i);} //  condition ==.

How come it satisfies ? You got the point right ?

If you are unable to get adding @Cruncher's comment

To explain this a little better: i < args.length and i == args.length are mutually exclusive conditions. That is, if one is true then the other is false. You need both to be true to get inside this if.

i is a number. args.length is also a number. Your mistake is that you are using only one = to check for equality instead of == . = is used to assign new values. So, i will get the value of args.length and your if cannot check if a number is true or false.

Also, you should use for-each instead of classic for.

for (String argument : args) {
  // do something with one argument
}

Even if you need the index, you will have less errors in your code if you use the for-each. For index, I am using another int/long variable that gets incremented inside the loop.

System.out.println(args.length); prints repeatetly because it's in the for-loop.

If it should be printed only once and on top it has to be before the for-loop.

Furthermore because the loop runs from i = 0 to i < args.length the expression i == args.length will never be true inside the loop.

First of all, your logic is a little more complicated than it needs to be. You can print out the number of arguments by simply passing args.length to println. Then, if you want to print out certain arguments in an order of your choice, you can make a for loop for each, which will find if such an argument exists, and if so print it.

Example:

for (int i=0; i < args.length; i++)
{
if (args[i].startsWith("message"){System.out.println(args[i]);}
}

for (int i=0; i < args.length; i++)
{
if (args[i].startsWith("this"){System.out.println(args[i]);}
}

There are more elegant ways to print out arguments in the order you desire where they match certain terms, but this will work.

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