0

In this program I am taking data from the user. If a user enters anything else rather than a integer so exception will raise. I catched the exception and in the catch block I called data() method again so that user will enter right data . But whenver I entered wrong data program stuckked in infinte loop. I am new to programming, please help.

static Scanner sc;
static double n1;
static double n2;
static String operation;

static void data()
{
    try
    {
    System.out.println("Enter first number");
    n1=sc.nextInt();
    sc.nextLine();

    System.out.println("Enter second number");
    n2=sc.nextInt();
    sc.nextLine();

    System.out.println("Enter operation you want to perform");
    operation=sc.nextLine();
    }

    catch(Exception e)
    {
        System.err.println("Wrong data entered!! \n Please enter right data");
        data();
    }

}

0 Answers0