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??

8th Aug 2016, 4:27 AM
Ka Ho Hui
Ka Ho Hui - avatar
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(); } }
8th Aug 2016, 4:58 AM
Mohammad Reza Karimi
Mohammad Reza Karimi - avatar
+ 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
8th Aug 2016, 6:06 AM
James
James - avatar
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?
8th Aug 2016, 4:41 AM
James
James - avatar
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.
8th Aug 2016, 5:40 AM
Ka Ho Hui
Ka Ho Hui - avatar
0
Exactly as you say, James. I was having the wrong expectation you described. Thanks!!
8th Aug 2016, 6:44 AM
Ka Ho Hui
Ka Ho Hui - avatar