Java program to accept 5 integers passed as arguments while executing the class

Published: 11 February 2020
on channel: Basic Code
1,665
22

Write a program to accept 5 integers passed as arguments while executing the class. Find the average of these 5 nos. Use ArrayIndexOutofBounds exception to handle situation where the user might have entered less than 5 integers.
---------------------------------------------------------------------------------------------------------------------------
public class Test {
public static void main(String[] args) {
double avg=0;
int a[]=new int[args.length];
int num;
for (int i = 0; i less than args.length; i++) {
num = Integer.parseInt(args[i]);
a[i] = num;
avg += num;
System.out.println(“Number=”+num);

}

try {
if(a.length!=5) {
throw new ArrayIndexOutOfBoundsException();
}
else {
avg = avg/a.length;
System.out.println(avg);
}
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println(e+": Please pass 5 integers");
}
}
}


On this page of the site you can watch the video online Java program to accept 5 integers passed as arguments while executing the class with a duration of hours minute second in good quality, which was uploaded by the user Basic Code 11 February 2020, share the link with friends and acquaintances, this video has already been watched 1,665 times on youtube and it was liked by 22 viewers. Enjoy your viewing!