2014年5月18日星期日

Scanner class throws java.util.NoSuchElementException: No line found abnormal

 This post last edited by the using_name on 2014-05-15 20:22:59
scanner object read an entire line of information in different places in the program , each after use very natural too close to call a method to switch off , when the second scanner object calls nextLine appears NoSuchElementException: No line found exception.
object if the first one does not turn off the scanner , you will not complain. Want to know why.
personally think that this is all of us will often make the mistake , but the Internet can not find an explanation. I hope the great God appear.

simplify the code shown below
import java.util.Scanner;


public class TempTest{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
//if(sc.hasNext())
String s=sc.nextLine();
sc.close();

Scanner sc2=new Scanner(System.in);
sc2.nextLine();
sc2.close();
}
}

------ Solution ------------------------------------- -------
Java are executed sequentially after you perform on your behalf to shut down the flow . close (), you go to call you close the stream has been ; obviously unrealistic
My suggestion is that you do a few methods which contains the input stream , then you can call the main inside the
------ Solution ------------ --------------------------------
System.in main problem is not re- opened, it can end up in the program closed

final Scanner scanner=new Scanner(System.in);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
scanner.close();
}
});

------ Solution ------------------------------------- -------
you use Eclipse , right ? Eclipse run java program using javaw, not associated with the console , so System.console () is null
If you like
------ For reference only ------------------------- by running java command cmd --------------
sc.close () will System.in also turned off, so ...
------ For reference only --- ------------------------------------
public void close () Closes this scanner .
If this scanner has not yet closed, and its underlying readable also implements the Closeable interfaces , close the readable method will be called.
System.in is InputStream object , and turn off after no longer open
------ For reference only ------------------- --------------------
then how to achieve it in a different place to read ? With the Scanner class
------ For reference only ----------------------------------- ----

If you have to use System.in, so not all read before the finish do not close the Scanner
------ For reference only -------- -------------------------------

If you have to use System.in, then not all Do not close the Scanner finished before reading  
That first Scanner objects do not close , and then apply the second object.
see youwarning not always feel great. . .
Security Scanner if not there is no other better way to achieve multiple reads input in the console , with the hope of code oh
------ For reference only ------ ---------------------------------
can also use this
BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
The reader can not be closed , but it is not never closed warning
------ For reference only --------------------- ------------------

way too much trouble , not only to create objects of trouble, but also add try ... catch.
Is not there an easier way?
I think there should be a common approach to deal with such issues now !
------ For reference only -------------------------------------- -

I specifically wrote a class
class input
{
final static Scanner sc=new Scanner(System.in);
}
After all call each
input.sc, and finally the it off. That's all.
Thank you very much , your answer too professional !
------ For reference only -------------------------------------- -

I was a college rookie , would like to ask a question about input
Console cons = System.console (); Why create console object is null

For example, the following code will not execute successfully
Console cons;
char[] passwd;
if ((cons = System.console()) != null &&
(passwd = cons.readPassword("[%s]", "Password:"))!= null) {
     ...
java.util.Arrays.fill(passwd, ' ');
 }

------ For reference only ----------------------------------- ----
lesson
------ For reference only -------------------------------------- -

Although this problem is very low , but if you do not tell , then throw themselves can not think of a moment .
I learn java very short time, a lot of the problem is not clear , thank you advise, thank you very much.

没有评论:

发表评论