0
Resource leak?
I tested the codes in both the example and the quiz, but got an error on the line: Scanner myVar = new Scanner(System.in); saying âResource leak: âmyVarâ is never closedâ What to do now??
5 Respostas
+ 2
try this code
run correct
import java.util.Scanner;
class MyClass {
public static void main(String[ ] args) {
Scanner myVar = new Scanner(System.in);
System.out.println("you entered : \n" + myVar.nextLine());
myVar.close();
}
}
+ 1
So, nothing should be displayed in console with the code until after you put in input. What happens when you click in the console box and type in something, then press enter?
I'm thinking you may be expecting a pop-up box like in this apps compiler. That's actually a program and not what you'd expect unless you built a pop-up box to accept input. Normally, you are given the blank console, and you add necessary input in it
0
myVar.close() after youve gathered the input.
Though, that shouldn't be a problem in the example code in the input section of this app. hmm, I'd have to see all your code to know exactly what's going on, or are you testing the code in a different IDE?
0
Thanks for helping.
I tested my code, and also another code with the line you guys provided, âmyVar.close()â, added, through cmd. These were successful. It prompts my input and display my input successfully.
However, it does not work (neither mine or the above code) in Eclipse IDE for Java Developers. Once run is pressed, it does not prompt for my input and nothing is displayed in the console. Though, with the line âmyVar.close()â added, the previously mentioned error no more appeared in the âProblemsâ tab.
0
Exactly as you say, James. I was having the wrong expectation you described.
Thanks!!