Scanner in = new Scanner(System.in); String strval; int sum = 0; int count = 0; int n; while( true ) { System.out.println("Enter a integer value or 'quit' to stop"); strval = in.next(); if ( strval.startsWith("q") ) { break; } try { n = Integer.parseInt(strval); count++; } catch(NumberFormatException e) { System.out.printf("%s is not a valid integer. Try again.\n", strval); continue; } sum += n; } System.out.printf("sum of %d input integers is = %d\n", count, sum);