Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Is is common knowledge that people are quite often incompetent, and will provide

ID: 3707061 • Letter: I

Question

Is is common knowledge that people are quite often incompetent, and will provide wonderful software with the incorrect input. For example, if a program expects to find a list of integers on its command line, they will regularly give words like "cat" instead.

Write a program that expects to find a list of integers on its command line, and is robust against incorrect user input. Each correct integer found must be wrapped in an Integer object, and a Vector must be used to store the objects. After all the (correct integer) command line arguments have been stored in the vector, the contents of the vector must be output.

The program must:

Loop through the command line Strings in the main method.

For each command line String, call a "converter" method to try build an Integer object from the string.

The converter method must try to convert its string argument (sent by the main) to a primitive integer. If that fails a NumberFormatException is thrown up to the main. Otherwise the primitive integer is printed out, and an Integer object created from the primitive integer is returned from the method up to the main method.

The call from the main to the converter method must be within a try-catch block. If the object is built successfully it is added to the vector (done in the main method). If a NumberFormatException exception is thrown by the method, an appropriate error message must be output from within the catch part.

Here what a sample run should look like (with the keyboard input shown in italics) ...

You must ...

Implement the program in Java

Explanation / Answer

Solution:

import java.util.*;

public class ExceptionalCommandLine{

public static void main(String []args)
{
int i,r;
Vector <Integer> v = new Vector<Integer>(); //vector declaration
for(i=0;i<args.length;i++)
{
try{
r=converter(args[i]); // call to converter function
System.out.println("Converter method says integer OK-"+r);
v.add(new Integer(args[i])); // adding integer function to vector
}
  
catch (Exception e)
{
  
System.out.println("Catch block says the argument " "+args[i]+" " is ignored because " " +args[i]+ """);
  
}
  
}
System.out.println("Vector contents are");


for(i=0;i<v.size();i++) // v.size() is used to get vector size


System.out.println("item "+i+" is "+v.get(i)); // v.get(i) is used to get element at ith position
}
public static int converter(String s)
{
int res;
res=Integer.valueOf(s); // getting integer value of string

return res; // return result
}
}

Output:

We should save the file as ExceptionalCommandLine.java.

Then compile it using

>javac ExceptionalCommandLine.java

Here we are giving input using commandline arguments.So we enter

> java ExceptionalCommandLine 1 2 A B

output will be

Converter method says integer OK-1
Converter method says integer OK-2
Catch block says the argument " A " is ignored because " A"
Catch block says the argument " B " is ignored because " B"
Vector contents are
item 0 is 1
item 1 is 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote