In: Computer Science
Variation on the ubiquitous "hello, world!" program. One argument from the command line may be passed to the main method. If an argument is to be used, it must have a value of "-h" or "-w". If "-h" is passed to the main method as an argmument, the output of the program is"Hello". If "-w" is passed as an argument to the main method, the out of the program is "World". If no arguments are passed to the main method, The output is "Hello World". If the above conditions are not met then the output describes the correct usage of the program.
Upload your source code, executable/runnable .jar, and a screenshot of your output.
You should name your program "helloworld2".
Example of compiling and running.
$ javac HelloWorld2.java
$ java HelloWorld2 -h
Hello
$ java HelloWorld2 -w
World
$ java HelloWorld2
Hello World
$ java HelloWorld -blah
Usage: java HelloWorld2 [-h|-w] or no option.
------------------------------------------------------------------------------------------------
This is my HelloWorld2.java
public class HelloWorld2 {
public static void main(String[] args) {
String Hello = "-h";
String World = "-w";
String input = "
";
if (args.equals(Hello))
{
System.out.println("Hello");
}
else
if(args.equals(World)){
System.out.println("World");
}
else if(args.equals(input))
{
System.out.println("Hello World");
}
else{
System.out.println("type '-h' or '-w' or no option'");
}
}
}
--------------------------------------------------------------------------------------
I seem to be running into an issue where my program does not take any arguments in the command line? Please help!
If you have any doubts, please give me comment...
public class HelloWorld2 {
public static void main(String[] args) {
String Hello = "-h";
String World = "-w";
if(args.length>0){
if (args[0].equals(Hello)) {
System.out.println("Hello");
} else if (args[0].equals(World)) {
System.out.println("World");
} else {
System.out.println("Usage: java HelloWorld2 [-h|-w] or no option.");
}
}
else{
System.out.println("Hello World");
}
}
}