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);
    }
}

ehm ...

try{
//yourCode
}
catch(YourSpecificException yse){
// handleException
}
catch(YourOtherSpecificException yose){
// handleException
}
catch(Exception e){
// handle all other exceptions, just in case
}

Thanks, below is what I did, the try and catch works but I want to loop the program if the input error is occurred, I have tried and having this one error: C:\java-workspace\ElseElseIf.java:46: error:

int cannot be dereferenced exitChoice = input.next(); 1 error

for which I have no idea, any help will be appreciated!

import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.*;

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;
        boolean exit = true;
        Scanner scanner =new Scanner(System.in);
    while(!exit);
    {
		try
		{
        //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;
	}

		catch(InputMismatchException exc)
				{
					System.out.println("Invalid input");
					return;
				}
			}
					String exitChoice;
					System.out.print("\nDo you really want to exit?(Y/N): ");
					exitChoice = input.next();
					if(exitChoice.equals("Y") || exitChoice.equals("y"))
					exit = true;
					else
					exit = false;

        //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);

    }
}

sorry

Then what can be assigned? Can you please help with looping the program again if error is caught?

create a new String object and add that int to the value.
or just putting: String.valueOf(input);
would do the trick.

Please ignore my previous post - brain was in reverse. Try this instead:

exitChoice = input.next();

input is an integer variable, so doesn't have a next() method. Looks like you confused "input" and "scanner" when you typed that line.

either that, or he wants the value of the input as (part of) a String object :)

I'm still confused can you please write the actual part of the code please?

int cannot be dereferenced exitChoice = input.next(); 1 error

should be

exitChoice = scanner.next();

Thanks all for the help!! Really appreciate it... But still facing the problem, I wanted it to loop until user inputs the valid input, the program below catches the error but is not looping again. PLEASE HELP.

import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.*;

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;
        boolean exit = true;
        Scanner scanner =new Scanner(System.in);
    while(!exit);
    {
		try
		{
        //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;
	}

		catch(InputMismatchException exc)
	    {
	    System.out.println("Invalid input");
	    return;
	    }

		String exitChoice;
		System.out.print("\nDo you really want to exit?(Y/N): ");
		exitChoice = scanner.next();
		if(exitChoice.equals("Y") || exitChoice.equals("y"))
		exit = true;
		else
		exit = false;




        //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);

    }

}
}

try by removing the

return;

statement in your catch block

Thanks for the help, I tried removing the

return;

statement form the catch block, but that gave me below mentioned errors:

C:\java-workspace\ElseElseIf.java:57: error: variable number1 might not have been initialized
        largest = number1;
                  ^
C:\java-workspace\ElseElseIf.java:60: error: variable number2 might not have been initialized
        if(largest < number2)
                     ^
C:\java-workspace\ElseElseIf.java:64: error: variable number3 might not have been initialized
        if(largest < number3)
                     ^
C:\java-workspace\ElseElseIf.java:68: error: variable number4 might not have been initialized
        if(largest < number4)
                     ^
C:\java-workspace\ElseElseIf.java:72: error: variable number5 might not have been initialized
        if(largest < number5)
                     ^
5 errors

initialize default values before the try-catch block, or ==>

public static void main(String[] args){
try{
// All your code here
}
catch(Exception e){
System.out.println("Error: " + e.getMessage());
}
}

Still No Success :(

don't just say: no success
say:

this is what I'm doing now

current code

what you expect

what you get

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.