Forums Register Login

Use enf of file character in Scanner

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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().
+Pie Number of slices to send: Send
 


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.
+Pie Number of slices to send: Send
You would do well to read the Scanner API documentation, and see what those methods do and what those methods return. If you get the bit with ==-1 in to compile, I shall be very surprised!
+Pie Number of slices to send: Send
And, as far as I know, you only get an end-of-file character of -1 if you use an unbufferred Reader. Scanner does all the buffering, so it doesn't return -1 for end-of-file.
Uh oh, we're definitely being carded. Here, show him this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3669 times.
Similar Threads
Scanner...K&B p.no:484
Need Help Creating A Loop!
Scanner hasNext() and indexoutofboundsexception
Two Dimensional array declaration in Java
Study Material for Scanner and Fromatter
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:37:57.