import java.util.Scanner;
public class Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int arr[] = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("Maximum value of array is : " + max);
System.out.println("Minimum value of array is : " + min);
}
}
When I compile this code it keeps going on until I terminate it and when done so it gives the exception:
Exception in thread "main" java.util.InputMismatchException
In my opinion,that's normal phenomenon,check the api document of InputMismatchException,we can find below description:
Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.
In your code we can see
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
So when you terminate it, the input signal can not conver to int
,thus the exception will thrown.