The below program works fine, but I want to add data exception using try and catch, can anyone please help how and form where I can start it?
import java.util.Scanner;
public class ElseElseIf {
public static void main(String[] args) {
int number1,number2,number3,number4,number5;
int largest, smallest;
int twoDigitNumbers = 0;
int threeDigitNumbers = 0;
int sumOfTwoDigitNumbers = 0;
int sumOfThreeDigitNumbers = 0;
int input = 0;
Scanner scanner =new Scanner(System.in);
//input 5 numbers
System.out.println("Enter 1st number: ");
input = scanner.nextInt();
number1 = input;
System.out.println("Enter 2nd number: ");
input = scanner.nextInt();
number2 = input;
System.out.println("Enter 3rd number: ");
input = scanner.nextInt();
number3 = input;
System.out.println("Enter 4th number: ");
input = scanner.nextInt();
number4 = input;
System.out.println("Enter 5th number: ");
input = scanner.nextInt();
number5 = input;
//print largest number
largest = number1;
smallest = number1;
if(largest < number2)
largest = number2;
else if(smallest > number2)
smallest = number2;
if(largest < number3)
largest = number3;
else if(smallest > number3)
smallest = number3;
if(largest < number4)
largest = number4;
else if(smallest > number4)
smallest = number4;
if(largest < number5)
largest = number5;
else if(smallest > number5)
smallest = number5;
//print largest number
System.out.println("Largest number: ");
System.out.println(largest);
//print smallest number
System.out.println("Smallest number: ");
System.out.println(smallest);
if(number1 >= 10 && number1 < 100)
{
twoDigitNumbers++;
sumOfTwoDigitNumbers = sumOfTwoDigitNumbers + number1;
}
else if(number1 >= 100 && number1 < 1000)
{
threeDigitNumbers++;
sumOfThreeDigitNumbers = sumOfThreeDigitNumbers + number1;
}
if(number2 >= 10 && number2 < 100)
{
twoDigitNumbers++;
sumOfTwoDigitNumbers = sumOfTwoDigitNumbers + number2;
}
else if(number2 >= 100 && number2 < 1000)
{
threeDigitNumbers++;
sumOfThreeDigitNumbers = sumOfThreeDigitNumbers + number2;
}
if(number3 >= 10 && number3 < 100)
{
twoDigitNumbers++;
sumOfTwoDigitNumbers = sumOfTwoDigitNumbers + number3;
}
else if(number3 >= 100 && number3 < 1000)
{
threeDigitNumbers++;
sumOfThreeDigitNumbers = sumOfThreeDigitNumbers + number3;
}
if(number4 >= 10 && number4 < 100)
{
twoDigitNumbers++;
sumOfTwoDigitNumbers = sumOfTwoDigitNumbers + number4;
}
else if(number4 >= 100 && number4 < 1000)
{
threeDigitNumbers++;
sumOfThreeDigitNumbers = sumOfThreeDigitNumbers + number4;
}
if(number5 >= 10 && number5 < 100)
{
twoDigitNumbers++;
sumOfTwoDigitNumbers = sumOfTwoDigitNumbers + number5;
}
else if(number5 >= 100 && number5 < 1000)
{
threeDigitNumbers++;
sumOfThreeDigitNumbers = sumOfThreeDigitNumbers + number5;
}
//print total counts of 2 digit and 3 digiti numbers
System.out.println("Total count of 2 digit numbers: ");
System.out.println(twoDigitNumbers);
System.out.println("Total count of 3 digit numbers: ");
System.out.println(threeDigitNumbers);
//print sum of all 2 digit numbers
System.out.println("Sum of all 2 digit numbers: ");
System.out.println(sumOfTwoDigitNumbers);
//print sum of all 3 digit numbers
System.out.println("Sum of all 3 digit numbers: ");
System.out.println(sumOfThreeDigitNumbers);
}
}