Hello, there was a question: for example, created a stream
Scanner sc = new Scanner(System.in); I will write in the code after that sc.close (); so that he does not use resources. In this case, if necessary, I can then create a stream with the same name, after closing, because it is deleted? For example, the following code:
public class Main { public static void main(String[] args) { Scanner sc; int n; System.out.print("Введіть кількість квадратів: "); while (true) { sc = new Scanner(System.in); if (!sc.hasNextInt()) { System.out.print("Некоректний тип введеного числа! Повторіть спробу: "); } else { n = sc.nextInt(); sc.close(); break; } } Quadrate[] quadrates = new Quadrate[n]; for(int i = 0; i < quadrates.length; i++) { System.out.printf("Введіть сторону %d + 1 - го: ", i); double v = sc.doubleNextInt(); quadrates[i] = new Quadrate(v); } } } How to implement this code correctly?
scanner.close()will closeSystem.inat the same time, after which the new scanners will no longer be able to read anything from there. Well, to recreate the scanner, really, each time is not necessary, one copy at the beginning of the method is enough. - zRrr