- In the editor of your choice, create a new file named Chaining.java. For example, from the command-line you could just type the following (if using Windows).
> notepad Chaining.java
Or the following if using Linux.
$ nano Chaining.java
- Type the following code. Be certain to name the class Chaining.
public class Chaining { public static void main(String[] args){ int x = Integer.parseInt(args[0]); if(x > 0){ System.out.println("x is positive"); } else if (x < 0) { System.out.println("x is negative"); } else { System.out.println("x is zero"); } } }
- Compile using javac and then run using the following statement.
Of course, if using Eclipse, use Eclipse’s run button to run the program. But, remember though, you must set the run-time arguments in the Run Configuration if using Eclipse. I do not explain how to do that here, so you are better off using Notepad or Nano if you do not how to do that. Here are instructions though. Also, in the next try-this, Flag Variables and the Return Statement, you set Eclipse’s run-time configuration and instructions are provided.
$ java Chaining 3 x is positive $ java Chaining -2 x is negative $ java Chaining 0 x is zero
For more information on nested conditional statements refer to the following video by LearningLad.