[Java Program] You are to create a program that properly handles two types of in
ID: 3746701 • Letter: #
Question
[Java Program]
You are to create a program that properly handles two types of input-- command line arguments and standard input. The purpose of this exercise is to ensure that you can effectively handle both types of input and that you clearly understand the differences. In particular, the program will perform as follows:
There are three possible command line arguments for this program: option1, option2, and option3. Each option appears following a single dash ('-'). A word of text follows certain options. Your program must read the actual arguments present and store any additional data associated with them.
The options appear on the command line as follows:
option1: -o
option2: -t
option3: -h
A word always follows option1 and option2. There is no data following option3.
Any, all, or none of these options may be present on the command line; moreover, they may be present in any order. Your program must properly handle all possible combinations. For example, option3 (-h) may be present, followed by option1 (-o), while option2 is not present on the command line.
In addition to command line arguments, your program must also read data from standard input.
Your program will read data from standard input one line at a time. Your program must continue reading until there is no more data from standard input.
When your program starts, it must output the heading, "Standard Input:" (without quotation marks).
As your program reads a line from standard input, it will output the line immediately to standard output.
Once all data has been read from standard input and output accordingly, your program will output the heading, "Command line arguments:" (again, without quotation marks); your program will then display a list of the command line options found, along with their data, in numeric order. In other words, option1 must be displayed first, if present, then option2, and finally option3. The heading must be output even if there are no command line arguments. DO NOT DISPLAY AN OPTION IF IT IS NOT PRESENT ON THE COMMAND LINE.
Example 1: Execution on Windows (< Windows 10)
Java:
Standard input entered: zebra, hyena, CTRL-z
Output as it should appear:
Example 2: Execution on Linux, Mac, or Windows 10 (depending on configuration)
Java:
Standard input entered: hippopotamus, CTRL-d
Output:
Explanation / Answer
Please save the below code as InputProgram.java
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class InputProgram {
public static void main(String ... argv) {
Scanner sc = new Scanner(System.in);
// reading standard input and printing to console
System.out.println("Standard Input:");
while(sc.hasNext()) {
System.out.println(sc.next());
}
//closing the scanner
sc.close();
//parsing the argv
parse(argv);
}
public static void parse(String s[]) {
System.out.println("Command line arguments:");
String t="",h="",o="";
//added all args to array list
List<String> list = Arrays.asList(s);
//checking for o option
if(list.contains("-o")) {
int index = list.indexOf("-o");
String temp ="";
try{
//getting the value
temp=list.get(index+1);
}
//if no value for o then exiting
catch(Exception e) {
System.out.println("value required for option -o");
System.exit(0);
}
if(temp.equals("-h")||temp.equals("-t")){
System.out.println("value of -o is needed ");
System.exit(0);
}
else {
o=temp;
}
}
//similar as above
if(list.contains("-t")) {
int index = list.indexOf("-t");
String temp ="";
try{
temp=list.get(index+1);
}
catch(Exception e) {
System.out.println("value required for option -t");
System.exit(0);
}
if(temp.equals("-h")||temp.equals("-o")){
System.out.println("value required for option -t ");
System.exit(0);
}
else {
t=temp;
}
}
if(list.contains("-h")) {
int index = list.indexOf("-h");
String temp = "";
try{
temp=list.get(index+1);
}
catch(Exception e) {
}
if(temp.equals("-t")||temp.equals("-o")){
}
else {
h=temp;
}
}
//checking and printing options
if(!o.isEmpty()) {
System.out.println("Option 1: "+o);
}
if(!t.isEmpty()) {
System.out.println("Option 2: "+t);
}
System.out.println("Option 3: "+h);
}
}//end of program
//output
~ java InputProgram -o zebra -h
Standard Input:
Lion
Lion
Option 1: zebra
Option 3:
~ java InputProgram -o zebra -h -t dog
Standard Input:
hippo
hippo
Option 1: zebra
Option 2: dog
Option 3:
//Please do let me know if u have any concern...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.