Page 329 - computer science (868) class 11
P. 329

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.

                 11.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;





                                                                                                                       327
                                                                                                    Basic Input/Output   327
   324   325   326   327   328   329   330   331   332   333   334