+ 1
Scanner input function not working
This code is not working in Eclipse ide import java.util.Scanner; class MyClass { public static void main(String[ ] args) { Scanner myVar = new Scanner(System.in); System.out.println(myVar.nextLine()); } }
9 Answers
+ 4
The problem is program is not asking any input? what does leaking resources mean? Please i am a new guy into programming.
+ 1
! appears in the ide, and if i move mouse over it, it says myVar is never closed. I clicked the Scanner.class error and it says the JAR file has no source attachment.
0
What do you mean by not working? Please post the error.
EDIT
I think it is actually a simple warning, not an error. So you should still be able to run the code.
The IDE is just telling you that you have an open stream (you read data from the Keyboard) that you never close. Eventually it will still be closed by the operating system, but in the meantime you are leaking resources.
EDIT (2)
Leaking resources means wasting (no longer used) resources. This is a typical occurrence when you need to transfer data via a stream or a socket (input/output with files, Internet connections, whatever). First you open your data channel, then when you are done using it you should close it. Just like you would close a water faucet after using it.
0
to close it write myVar.close();
if you want that it open a console for running. It will not open. Did you even enter anything? Sorry for being a little rude.
0
Please watch some youtube video on topic my first java program in Eclipse. It will show you what was wrong. If it does not work try to reinstall Eclipse. Maybe there is a problem in installation.
0
My problem is it is not prompting any user input box!!
0
The problem resolved... :D
0
if you want the user to input
then the code shold look like this
import java.util.Scanner;
public class MyClass{
public static void main(String[] arg){
Scanner myVar = new Scanner(System.in);
String st = myVar.nextLine();
}
}
0
hey, what you simply do is type the input in the console area.i.e type whatever input.. if you're using the nextLine(type a word) and press enter,the input appears green when typed, then Press enter. the system outputs whatever word you typed. hope this clarifies you.