Title: PrintStream Scanner Examples
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

PrintStream Scanner Examples

In cps161 we had a lot of code that had statements like:

System.out.println("Enter some input");
Scanner keyboard = new Scanner(System.in);

String s = keyboard.nextLine();

To better understand this, we should look at the System class in the java.lang package to find the following:


static PrintStream 	err;      //The "standard" error output stream.
static InputStream 	in;       //The "standard" input stream.
static PrintStream 	out;      //The "standard" output stream.


In the Unix tradition applications have always had the "standard I/O streams" of stderr, stdin, and stdout. By default stderr and stdout default to the screen and stdin defaults to the keyboard. The java equivalents are the static classes err, in, and out.

We will want to use the Scanner class and the PrintStream classes for more than the keyboard and the screen. To start with they are useful for reading and writing disk files. Eventually we want to use these classes for reading and writing networking data to the internet.

For the Scanner class in cps161 we called methods like nextLine(), next(), and nextInt(). These calls would wait until the user entered input and hit the Enter key. To more generally use the Scanner class we need to become aware of some more methods: hasNextLine(), hasNext(), and hasNextInt().

To demonstrate some of these features, we have the following example:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.NoSuchElementException;
import java.util.Scanner;


public class Printstream_Scanner_Examples {

    void improperCopyByLine(Scanner scanner, PrintStream printStream)
    {
        while (true)
        {
            String s= scanner.nextLine();
            printStream.println(s);
        }
    }
    void improperCopyByLine2(Scanner scanner, PrintStream printStream)
    {
        try{
            while (true)
            {
                String s= scanner.nextLine();
                printStream.println(s);
            }
        }
        catch(NoSuchElementException e)
        {
            System.out.println("You just ran out of data from your scanner");
        }
    }

    void copyByLine(Scanner scanner, PrintStream printStream)
    {
        while (scanner.hasNextLine())
        {
            System.out.println("hasNextLine==true");
            String s= scanner.nextLine();
            printStream.println(s);
        }
    }
    void copyByString(Scanner scanner, PrintStream printStream)
    {
        while (scanner.hasNext())
        {
            System.out.println("hasNext==true");
            String s= scanner.next();
            printStream.println(s);
        }
    }
    public static void main(String[] args) {
        Printstream_Scanner_Examples pse = new Printstream_Scanner_Examples();
        Scanner keyboard = new Scanner(System.in);

// Reading from the keyboard with the Scanner class

        pse.improperCopyByLine(keyboard, System.out);
        //pse.copyByLine(keyboard, System.out);
        //pse.copyByString(keyboard, System.out);

// Reading from a File with the Scanner class

        try {

// A FileInputStream extends InputStream.
// Note that System.in is an InputStream

            FileInputStream fi = new FileInputStream(args[0]);
            Scanner fileScanner = new Scanner(fi);
            //pse.improperCopyByLine(fileScanner, System.out);
            //pse.improperCopyByLine2(fileScanner, System.out);
            //pse.copyByLine(fileScanner, System.out);
            //pse.copyByString(fileScanner, System.out);

            fileScanner.close();

        }
        catch (FileNotFoundException e) {
            System.out.println("Couldn't find file: "+ args[0]);
        }
        catch (ArrayIndexOutOfBoundsException e){
            System.out.println("Did you forget to specify a command line argument for the filename");
        }



    }

}



You can use the following input file to read:


She sells sea shells down by
the sea shore.
Peter Piper picked
...


Last Updated: May 26, 2015 4:25 PM