Lecture 7 (Tychonievich) - System.in and System.out

Lecture Date: Wednesday, January 28

And now to begin the slow and steady part of the course.

Java Programs

  • Java code must be placed inside of a class.

  • In CS 1110 we will only deal with public classes; non-public classes are restrict which other program can use them.

  • Public classes must be placed in a file with the same name as the class; e.g., public class Kay must be in “Kay.java”.

  • Classes can fulfil three conceptual purposes:

    • They are programs if they contain public static void main(String[] args) {.
    • They can be libraries of methods we can use, like Math is; we’ll learn to write that kind of class a few weeks from now.
    • They can define new types of values we can use, like Turtle does; we’ll learn to write that kind of class during the latter half of the semester.

For now we are writing programs, which means each file will begin with

public class NameOfThisProgram {
    public static void main(String[] args) {

… all of our code will go here, and the program will close with

    }
}

The Console

Most interesting programs accept input from the user, do something based on that input, and then produce output. The simplest way of handling input and output in Java is with the console. The console is a text-based tool that has existed in some form since before computers were fast enough to have graphics of any kind.

The console is one of several tools that are accessed through the built-in Java class System. System contains an object named System.out that knows how to display text on the console and an object named System.in that knows how to retrieve what the user types on the console. System.out is fairly clever, and we’ll have our programs talk to it directly. System.in is not as useful, so we’ll make special objects called Scanners to act as go-betweens between us and System.in.

System.out

When we talk to System.out we’ll almost always use one of two methods. System.out.println( X ); means “Hey, System.out, would you display X and then advance to the next line?” while System.out.print( X ); means “display X, and I’m about to ask you to display more things that will go on the same line as X

  • System.out.println("Hi");
  • System.out.println(1110);
  • System.out.println(1110 + 1000);
  • System.out.print(“I am”);
    System.out.println(“ble about”);
    System.out.println(“daily”);

If we want to print several things on a line, the usual way is not to use a lot of print statements, but instead to concatenate the bits by adding them to some text (text is called a String in Java), like so:

  • System.out.println("CS " + 1110);
  • System.out.println("CS " + 30 * 37 + "-002");

Note that mixing adding text and numbers can give unexpected results:

  • System.out.println("CS " + 1000 + 110 );
  • System.out.println("CS " + (1000 + 110));
A Scanner for System.in

System.in speaks an obscure dialect we don’t want to learn, so we’ll speak to it through an interpreter or Scanner.

  • Scanner whateverWeWantToCallIt = new Scanner(System.in);

I’ll usually name the Scanner that talks to System.in keyboard, but that’s just because I like that name; if you like translator or interpreter or thingThatGetsInput or xyxxy, Java dosn’t care.

A Scanner looks at everything the user ever types and keeps its finger somewhere in that text. To understand it’s behavior, consider it’s method nextInt, which mean

  1. read the text following where your finger currently is located
  2. give me what you found as an int-type value (i.e., turn the text “123” into the number one hundred twenty-three)
  3. move your finger to right after the thing you just read

We’ll use four of Scanner’s methods initially:

  • int x = keyboard.nextInt(); reads a single integer number, for example, 12.
  • double x = keyboard.nextDouble(); reads a single non-integer number; for example, 3.45.
  • String x = keyboard.next(); read a single “word”: a sequence of letters, numbers, or symbols not including any “white space” like spaces or newlines; for example, "t3rs\_4-r".
  • String x = keyboard.nextLine(); reads everything from where the finger currently is up to the next newline (which is what the Enter key types) and returns it all; for example, " and so on.".

It is almost always a good idea to print a prompt before reading input through System.in. A propmpt is just some text you show the user so they know what you are expecting them to type.

  • Scanner keyboard = new Scanner(System.in);
    System.out.print(“Type a number: ”);
    int x = keyboard.nextInt();
    System.out.println(“You typed one-half of ” + (x * 2));

Warning: be careful about mixing nextLine and other next/nextInt/nextDouble commands. nextline reads from wherever the finger is to the end of that line; so if the user types ‘x’, \<Enter>, ‘y’ \<Space> ‘z’ \<Enter> into a program that uses next then nextLine, the next gives "x" but the nextLine gives what’s between the end of the “x” word and the next newline, which is nothing: "".

Warning: don’t hire two interpreters to talk to the same foreigner at the same time. Only make one Scanner for System.in in each program!

DecimalFormat

Usually we talk to System.out directly, but sometimes it doesn’t do what we want. For example, System.out.println(1.0 / 3.0); prints out a whole lot of 3s.

There is a special kind of object called a DecimalFormat who’s entire mission in life is to format decimal numbers. Each individual DecimalFormater likes to make numbers fit exactly one pattern. For example, new DecimalFormat("0.0") always puts exactly one decimal place while new DecimalFormat("0.00") always puts two.

  • DecimalFormat one = new DecimalFormat(“0.0”);
    System.out.println(one.format(1.0 / 2.0));
    System.out.println(one.format(1.0 / 3.0));
  • DecimalFormat three = new DecimalFormat(“0.000”);
    System.out.println(three.format(1.0 / 2.0));
    System.out.println(three.format(1.0 / 3.0));

There are a lot of more complicated DecimalFormats possible too, but we won’t need them in this course.

From lecture:

Drawings: organization of Java programs and how Scanner works

Code: Demo1.java, Demo2.java

Audio: