I'm using scanner to fetch input from inputstream(System.in) The challenge is: 1) After printing the values , the connection to inputstream is not closed i.e it is ready to accept input values from System.in when it is not needed, atleast in my case. 2) So how to make the scanner find the end of file from the input stream.
I want to know is there any way 1) to find the length of the input stream values we give.
2) Can we impose restrictions on the number of values scanner can parse in Scanner or in inputstream on number of values we can give.
When I try finding out EOF through usage of -1 like this
But the problem with this approach 1) It doesn't display all the values I enter through System.in 2) Old problem of non-return of control persists If someone could throw some light on this, I would appreciate it. Regards.
I think in your case to skip reading the input from user you can use break; to come outof loop if you don't need. Also better to use sc.close() instead of System.in.close().
Joined: Oct 06, 2008
I think in your case to skip reading the input from user you can use break; to come outof loop if you don't need.
On what condition, i can give break. If i know exactly the number of values the user should give, then it is possible But if I want the application to be dynamic, the user can give varied no of values each time he runs it. In that case what can we do.
Also better to use sc.close() instead of System.in.close().
If I use sc.close() in that place instead of System.in.close() it throws java.lang.IllegalStateException. By the way why we should not close the inputstream. Regards.