Question

In: Computer Science

Variation on the ubiquitous "hello, world!" program. One argument from the command line may be passed...

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!

Solutions

Expert Solution

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");

        }

    }

}


Related Solutions

A C program that accepts a single command line argument and converts it in to binary...
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
Write a C program called test that takes one command line argument, an integer N. When...
Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits
Write a C program that accepts a port number as a command line argument, and starts...
Write a C program that accepts a port number as a command line argument, and starts an HTTP server. This server should constantly accept() connections, read requests of the form: GET /path HTTP/1.1\r\n\r\n read the file indicated by /path, and send it over the "connect" file descriptor returned by the call to accept().
Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
IN PYTHON: Compose a recursive program to draw Sierpinski triangles. Use a command-line argument to control...
IN PYTHON: Compose a recursive program to draw Sierpinski triangles. Use a command-line argument to control the depth of the recursion.
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusi...
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 49 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to square all 60 array elements. You should divide this update task among the n threads as evenly as possible. Print the array both before and after the...
The following code takes in a command line argument and copies it into a buffer before...
The following code takes in a command line argument and copies it into a buffer before working on it. Explain in detail any problems that you see in the following C code and how you would fix it. Note only describe how you would fix it, do not actually rewrite or give me another version of the code. void bad_function(char *input) { char dest_buffer[32]; char input_len = strlen(input); if (input_len < 32) { strcpy(dest_buffer, input_len); printf(“Command line argument is %s.\n”,dest_buffer);...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to generate a random sequence of integers be- tween 0 and N – 1. Run experiments to validate the hypothesis that the number of integers generated before the first repeated value is found is ~√?N/2.
The program should be able to do the following: In Java accepts one command line parameter....
The program should be able to do the following: In Java accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file in part a into an array of integers. sort the integers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT