Java InputMismatchException

You can use a do-while loop instead to eliminate the first input.nextInt(). int students = 0; do { try { // Get input System.out.print(“Enter the number of students: “); students = input.nextInt(); } catch (InputMismatchException e) { System.out.print(“Invalid number of students. “); } input.nextLine(); // clears the buffer } while (students <= 0); // Do … Read more

Angular2 – catch/subscribe to (click) event in dynamically added HTML

Declarative event binding is only supported in static HTML in a components template. If you want to subscribe to events of elements dynamically added, you need to do it imperatively. elementRef.nativeElement.querySelector(…).addEventListener(…) or similar. If you want to be WebWorker-safe, you can inject the Renderer constructor(private elementRef:ElementRef, private renderer:Renderer) {} and use instead this.renderer.listen(this.elementRef.nativeElement, ‘click’, (event) … Read more

Why is declaration required in Java’s try-with-resource

Since Java 9 you can declare and initialize the variable used inside try-with-resources outside the block. The only additional requirement for variable is that it has to be effectively final. So now it is possible to do: CloseableResource thing = methodThatCreatesAThingAndDoesSomeSideEffect(); try (thing) { // do some interesting things } thing.collectSomeStats(); Hope it helps.