Mam taki kod, który DZIAŁA
import java.util.Scanner;
public class TEST {
public static void main(String[] args) {
System.out.println("WPROWADZ INT");
Scanner scanner = new Scanner(System.in);
int xxx;
xxx = scanner.nextInt();
scanner.close();
System.out.println(xxx);
}
}
Ale w linii z "new Scanner" NetBeans daje ostrzeżenie-podpowiedź "Convert to try-with-resources". Jak w to kliknę, to robi się tak:
import java.util.Scanner;
public class TEST {
public static void main(String[] args) {
System.out.println("WPROWADZ INT");
int xxx;
try (Scanner scanner = new Scanner(System.in)) {
xxx = scanner.nextInt();
}
System.out.println(xxx);
}
}
Wtedy program TEŻ DZIAŁA, ale nie rozumiem co było źle, po co ta zmiana i co to za samo try bez catch i co to jest to try-with-resources. I dlaczego skasowało scanner.close()? Wszystko zrobiło się inaczej niż w tutorialach.