Using Scanner to determine EOL in Java

The java.util.Scanner is widely used for scanning inputs from the System.in object. We can easily detect an EOL with the hasNext() method of this object. When we type in the EOL, the method return false.

Here is an example:

public class masnun {
 
		public static void main(String args[]) {
 
				java.util.Scanner input = new java.util.Scanner( System.in );
				System.out.println( "Press <ctrl+D> on Linux/Mac or <ctrl+Z> on Windows to exit :)" );
				System.out.println( "Enter a new line: " );
				while( input.hasNext() ) {
						System.out.println( "You typed: " );
						String line = input.nextLine();
						System.out.println( line );
 
						System.out.println( "Enter a new line: " );
 
						}
 
					}
 
 
}
This entry was posted in Blog Post and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">