Page 354 - Cs_withBlue_J_C11_Flipbook
P. 354

The different constructors of the Scanner class are given in the following table.

                                           Constructor                          Description

                              Scanner(System.in)                   It reads data from the system’s standard
                                                                   input device.
                              Scanner(String)                      It parses String object.
                              Scanner(File source)                 It reads data from a specified file.

              Let us use the above constructors to create an object of the Scanner class in the following way:

                  Scanner sc = new Scanner(System.in);
                  Scanner scan = new Scanner("Scanning a sentence separated by space");
                  File f = new File("STUD.DAT");
                  Scanner inp = new Scanner(f);
              To change or include more separators other than the default whitespace, the delimiter() method is used. Some other
              commonly used methods of the Scanner class are given in the following table.


                                           Method                               Description
                             nextInt()                            It reads an integer value.
                             nextFloat()                          It reads a float value.
                             nextDouble()                         It reads a value of type double.

                             nextLong()                           It reads a value of type long.
                             nextBoolean()                        It reads a boolean value.
                             nextLine()                           It reads a String.
                             next()                               It reads a word.
                             close()                              It closes a Scanner object.

              You have already learnt about these methods in Chapter 5 of this book.

              12.1.2 The PrintStream Class
              The PrintStream class is an important class that produces output. It converts output data of primitive type to text and
              writes it to the output stream. The overloaded constructors of the PrintStream class are given in the following table.

                                          Constructor                           Description

                             PrintStream(File file)               It creates a new print stream, without
                                                                  automatic line flushing, with the specified file.

                             PrintStream(File file, String csn)   It creates a new print stream, without
                                                                  automatic line flushing, with the specified file
                                                                  and charset.

                             PrintStream(OutputStream out)        It creates a new print stream.

              The PrintStream class belongs to java.io package. To use this class, we need to import the java.io package by using the
              following statement:

                  import java.io.*;
                     OR
                  import java.io.PrintStream;





                352352  Touchpad Computer Science-XI
   349   350   351   352   353   354   355   356   357   358   359