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

2. Write a program on the Linux system using the putty utility. The program shou

ID: 3571788 • Letter: 2

Question

2. Write a program on the Linux system using the putty utility. The program should get strings of data from the command line (that is, look for the data in the “args” array of strings). Use a loop to convert each of the strings in the array into an integer and add the number to a total. Print the total after all of the strings have been processed. The program should use a try-catch block to stop the program and display “Invalid data” if it cannot convert one of the strings into an integer. You should create the Java program using the nano editor. The input data should be a list of numbers on the line that runs the program.

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

public class ArgsProgram {

  

   public static void main(String[] args) {

      

       try{

          

           int total = 0;

          

           // iterating over args array

           for(int i=0; i<args.length; i++){

               total = total + Integer.parseInt(args[i]);

           }

          

           System.out.println("Total: "+total);

          

       }catch(NumberFormatException e){

           System.out.println(e.getMessage());

       }catch(Exception e){

           System.out.println(e.getMessage());

       }

   }

}

/*

Sample run:

Command line input: 12 43 2 1 6 78 4

Total: 146

*/

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