fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. try (Scanner sc = new Scanner(System.in)) {
  6. System.out.println("Enter the length of the Array: ");
  7. int n = sc.nextInt();
  8. double[] array = new double[n];
  9. double sum = 0;
  10.  
  11. // Taking input and calculating sum
  12. for (int i = 0; i < array.length; i++) {
  13. if (sc.hasNextDouble()) {
  14. array[i] = sc.nextDouble();
  15. sum += array[i];
  16. } else {
  17. System.out.println("Invalid input. Please enter a valid number.");
  18. // Consume invalid input to avoid infinite loop
  19. sc.next();
  20. i--; // Retry current index
  21. }
  22. }
  23.  
  24. // Finding maximum and minimum
  25. double max = array[0];
  26. double min = array[0];
  27. for (int j = 1; j < array.length; j++) {
  28. if (array[j] > max) {
  29. max = array[j];
  30. }
  31. if (array[j] < min) {
  32. min = array[j];
  33. }
  34. }
  35.  
  36. // Calculating average
  37. double avg = sum / array.length;
  38.  
  39. // Output
  40. System.out.println("The maximum element is " + max);
  41. System.out.println("The minimum element is " + min);
  42. System.out.println("The summation is " + sum);
  43. System.out.println("The average is " + avg);
  44. } catch (Exception e) {
  45. System.out.println("An error occurred: " + e.getMessage());
  46. }
  47. }
  48. }
  49.  
Success #stdin #stdout 0.18s 58808KB
stdin
Standard input is empty
stdout
Enter the length of the Array: 
An error occurred: null