The
Scanner class is a class in java.util package, which allows a user to read values of various types.The Scanner looks for tokens in the input. A token is a series of characters that ends with whitespace. A whitespace character can be a blank, a tab character, a carriage return, or the end of the file.For example, if we read a line that has a series of numbers separated by blanks, the scanner will take each number as a separate token.
Constructors
Scanner class has several constructors. Among them, the following are particularly useful.
Scanner object.
Instantiating a Scanner object by passing a File object as parameter CODE import java.io.File; OUTPUT Germany DESCRIPTION Here, we have instantiated a
System.in is an InputStream .
Scanner in = new Scanner(System.in); // Reading from the keyboard Let's now instantiate a Scanner object by passing a String as parameter.
Instantiating a Scanner object by passing a String as parameter CODE import java.util.Scanner; OUTPUT Germany DESCRIPTION Here, we have instantiated a Scanner object using a string ( THINGS TO TRY
|