Concepts / Writing Programs That Print Results with System.out

Writing Programs That Print Results with System.out

Console-style input/output means the computer and the user exchange lines of text, with System.out handling most output and TextIO handling input in many sample programs.

  • Programming

A Program That Asks for a Number

Imagine a small program that asks the user for a whole number and then prints the square of that number. A sample program of this kind is called PrintSquare in the textbook discussion this material is drawn from. It has to do two jobs. It must receive what the user types, and it must show its answer back on the screen. Both jobs involve text moving between a person and a machine, one line at a time.

The question this article answers is practical: when a program takes its input through TextIO, what has to be in place on your computer for that input to work at all? The answer is not found in the code inside the program. It is found in the files the program depends on. Keep that in mind as you read, because the rule for these programs is about files and folders rather than about writing clever statements.

Tracing One Exchange Between User and Console

The source describes this activity as console-style input and output. The computer and the user exchange lines of text. The user types a line, the program receives it, the program produces a result, and the result appears on the console. In a program such as PrintSquare, these exchanges happen in a fixed order: input comes in first, then output goes out.

Usertypes a whole numberProgramreads input and computesConsoleshows the result1. typed line via TextIO2. result via System.out3. result seen on screen
In what order do the typed input line and the printed output line pass between the user, the program, and the console?

Read the diagram as a short story with three moments. First, the user types a line, and that line travels into the program through TextIO. Second, the program prints its answer through the standard output object, System.out, and the answer reaches the console. Third, the user sees that answer on the screen. The exchange is a conversation carried out in lines of text, and each line has a clear direction.

Which Tool Handles Which Direction

The two directions of the exchange are handled by different tools. For output, almost all of these sample programs use the standard output object, System.out. For input, many of them use TextIO instead. TextIO is therefore the piece you rely on when the program needs to read what the user typed. A program that uses TextIO for input still prints its results with System.out, because the two tools cover different directions of the same conversation.

Two Files Needed at Different Moments

The source states a requirement in two parts. When you compile a program that uses TextIO, either TextIO.java or TextIO.class must be available. When you run the program, TextIO.class must be available. Each part matters at a different moment, which is why the two are easy to confuse. Compiling is the step where the program is translated, and running is the step where it is executed. The requirement changes between those two moments.

TextIO.javaacceptable at compile timeTextIO.classrequired at run timeTextIO.classacceptable at compile time
Which TextIO files must be present when the program is compiled, and which one must still be present when it is run?

What do you think happens?

When you run a program that uses TextIO, which TextIO file must be present?

  • TextIO.java only

  • TextIO.class

  • Neither file, because the program is already compiled

Reveal answer
Answer:

TextIO.class must be present when the program runs.

The source states that TextIO.class must be available at run time. The .java file is needed only in the compile-time choice, where either TextIO.java or TextIO.class is acceptable.

Where the textio Folder Must Sit

Because TextIO is defined in a package named textio, its files must sit in a particular place. The source says that TextIO.java and/or TextIO.class must be in a directory named textio, and that this textio directory must be in the same directory as the program that uses it. In other words, the program and the textio folder are siblings. Neither one is placed inside the other. The diagram below shows that arrangement as a nested structure.

Project folderPrintSquare programthe program that usesTextIOTextIO.javacompile time, or the .classfiletextio directorysame directory as theprogramTextIO.classneeded at run timecontainscontainscontainscontains
Which folder holds the program and the textio directory, and where do the TextIO files go relative to them?
Worked example

Arranging files for the PrintSquare program

A student has a program called PrintSquare that reads a whole number with TextIO and prints its square. The student wants to know where the TextIO files must go before compiling and running it.

  1. 1
    Identify the program

    PrintSquare uses TextIO, so the textio package requirement applies to it.

  2. 2
    Place the textio directory

    Create a directory named textio. The source requires this name because TextIO is defined in the package named textio.

  3. 3
    Put it beside the program

    Locate the textio directory in the same directory as the PrintSquare program, not inside some other folder.

  4. 4
    Check the compile-time files

    For compiling, either TextIO.java or TextIO.class must be in the textio directory.

  5. 5
    Check the run-time file

    For running, TextIO.class must be in the textio directory. Keeping the .class file there covers both moments.

Result

The program and the textio directory sit side by side, and the textio directory holds TextIO.class so that the program can run.

The same placement logic applies to the other sample programs named in the source. The source notes that Interest2, which calculates interest on an investment for one year based on user input, also uses TextIO for input. For that program too, the textio directory belongs in the same directory as the program. The rule does not change simply because the calculation is different.

Mistakes Beginners Make with TextIO Files

  • Common mistake

    Thinking TextIO is built in and needs no files

    Why it fails

    The source states that TextIO is a nonstandard class defined in a package named textio, so its files must be made available to the program.

    Do this instead

    Make sure the TextIO files are in place before you compile and run any program that uses TextIO.

  • Common mistake

    Placing the textio directory inside the program's own folder without checking the rule

    Why it fails

    The source says the textio directory must be in the same directory as the program that uses it, not nested beneath it.

    Do this instead

    Put the textio directory beside the program so that both sit in the same directory.

  • Common mistake

    Believing that only the .java file is needed for both compiling and running

    Why it fails

    At compile time either TextIO.java or TextIO.class is acceptable, but at run time TextIO.class must be present.

    Do this instead

    Keep TextIO.class in the textio directory, since it is the file required in both situations.

  • Common mistake

    Confusing the input tool with the output tool

    Why it fails

    TextIO handles input in many sample programs, while System.out handles most output. Treating them as the same thing hides which one a file-placement problem concerns.

    Do this instead

    When a file-placement problem appears, check the textio requirement for the TextIO input path, and leave the System.out output path as the standard output object.

Checking a Project Layout

MEDIUM

A project folder contains a program called Interest2 that reads user input with TextIO. The folder also contains a subfolder named textio, placed inside a different subfolder named src. The program sits directly in the project folder. Explain whether this layout satisfies the textio placement rule, and say which TextIO file must be present when the program runs.

Hints
  • Look at where the textio directory sits relative to the program, not just whether it exists.
  • The source says the textio directory must be in the same directory as the program that uses it.
  • Recall which TextIO file must be present at run time.

Key Takeaways

  • A TextIO program exchanges lines of text with the user: input arrives through TextIO, and results leave through System.out to the console.
  • TextIO is a nonstandard class defined in a package named textio, so its files must be made available to the program.
  • When compiling, either TextIO.java or TextIO.class must be available; when running, TextIO.class must be available.
  • The textio directory must sit in the same directory as the program that uses it, so the program and the textio folder are siblings.