Thanks to Norman we have the Hello World Tutorial in Part II of the Tutorial. So I thought it may be good to have the possibility to control in the command line when the System will say Hello World.
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
That was the first Tutorial. I will give us the chance to decide from the command line, if Hello World is printed. That we do with the If command:
public class Main {
public static void main(String[] args) {
if (args[0].equals( "Palardys_Tutorials_are_nice" )){
System.out.println("Hello World");
}
}
}
So what this Code is doing? The Variable args contains the command line args. this command line args are the arguments written in command line when starting the Program. The Line
```
if (args[0].equals( "Palardys_Tutorials_are_nice" ))
```
is looking if the command line arguments in the first position of the array (caution that is zero based) is equal to âPalardys_tutorials_are_niceâ.
This is like written here working. So if we would compile the Program after editing like described in the Tutorial II with this command:
java helloworld Palardys_Tutorials_are_nice
we would get our Hello World as Result.
But what if somebody now uses the Program without arguments? That would result in an error message. We enter the Array on Position 0. If it is empty the System complains about it:
Exception in thread âmainâ java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0.
So we have to find security for it: the user shall be able to use the Program with or without the command line args. To do that we have to take care about. So we use a second if condition for it.
if (args.length > 0) {
looks if the args Array has more then one entry. If not, the Hello World Program says nothing.
Now our Program looks like this:
public class Main {
public static void main(String[] args) {
if (args.length > 0) {
if( args[0].equals( "Palardys_Tutorials_are_nice" ) ) {
System.out.println( "Hello World" );
}
}
}
}
And how would it look in Xojo? Hereâs the Code in Xojo:
if args.count > 1 then
if args(1)="Palardys_Tutorials_are_nice" then
print("Hello World")
end if
end if
One difference: in Java there is the First Command Line Arg on Position 0 in the Array!