1

Does anyone know why this code is stuck in an infinite loop? Once ran without the input of an integer it repeats infinitely "Please enter an integer"

class ExceptionHandling {
    public static void main(String[] args) {
        int foo= getInteger();
    }

    public static int getInteger() {
        Scanner in = new Scanner(System.in);

        while(true)
        {
            int delta = 0;
            try
            {
                delta = in.nextInt();
                return delta;
            }
            catch (Exception ex)
            {
                System.out.println("Please enter an integer");
            } 
        }
    }
}
Martin
  • 14,189
  • 1
  • 26
  • 43

0 Answers0