0% found this document useful (0 votes)
3 views3 pages

Java Input and Output Basics

Chapter 3 discusses input and output in Java, focusing on the System class and the Scanner class for handling user input. It explains how to import the Scanner class and create an instance to read input from the keyboard. The chapter also outlines the structure of Java programs, including packages, classes, methods, and expressions.

Uploaded by

abraham.demsis
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java Input and Output Basics

Chapter 3 discusses input and output in Java, focusing on the System class and the Scanner class for handling user input. It explains how to import the Scanner class and create an instance to read input from the keyboard. The chapter also outlines the structure of Java programs, including packages, classes, methods, and expressions.

Uploaded by

abraham.demsis
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

34 Chapter 3 Input and output

The numbers and letters after the @ sign are the address of [Link],
represented as a hexadecimal (base 16) number. The address of a value is
its location in the computer’s memory, which might be di↵erent on di↵erent
computers. In this example the address is 685d72cd, but if you run the same
code you might get something di↵erent.

As shown in Figure 3.1, System is defined in a file called [Link], and


PrintStream is defined in [Link]. These files are part of the
Java library, which is an extensive collection of classes you can use in your
programs.

Figure 3.1: [Link] refers to the out variable of the System


class, which is a PrintStream that provides a method called println.

3.2 The Scanner class


The System class also provides the special value [Link], which is an
InputStream that provides methods for reading input from the keyboard.
These methods are not easy to use; fortunately, Java provides other classes
that make it easier to handle common input tasks.

For example, Scanner is a class that provides methods for inputting words,
numbers, and other data. Scanner is provided by [Link], which is a
3.2 The Scanner class 35

package that contains classes so useful they are called “utility classes”. Before
you can use Scanner, you have to import it like this:

import [Link];

This import statement tells the compiler that when you say Scanner, you
mean the one defined in [Link]. It’s necessary because there might be
another class named Scanner in another package. Using an import statement
makes your code unambiguous.

Import statements can’t be inside a class definition. By convention, they are


usually at the beginning of the file.

Next you have to create a Scanner:

Scanner in = new Scanner([Link]);

This line declares a Scanner variable named in and creates a new Scanner
that takes input from [Link].

Scanner provides a method called nextLine that reads a line of input from
the keyboard and returns a String. The following example reads two lines
and repeats them back to the user:

import [Link];

public class Echo {

public static void main(String[] args) {


String line;
Scanner in = new Scanner([Link]);

[Link]("Type something: ");


line = [Link]();
[Link]("You said: " + line);

[Link]("Type something else: ");


line = [Link]();
[Link]("You also said: " + line);
}
}
36 Chapter 3 Input and output

If you omit the import statement and later refer to Scanner, you will get a
compiler error like “cannot find symbol”. That means the compiler doesn’t
know what you mean by Scanner.

You might wonder why we can use the System class without importing it.
System belongs to the [Link] package, which is imported automatically.
According to the documentation, [Link] “provides classes that are funda-
mental to the design of the Java programming language.” The String class
is also part of the [Link] package.

3.3 Program structure


At this point, we have seen all of the elements that make up Java programs.
Figure 3.2 shows these organizational units.

Figure 3.2: Elements of the Java language, from largest to smallest.

To review, a package is a collection of classes, which define methods. Methods


contain statements, some of which contain expressions. Expressions are made
up of tokens, which are the basic elements of a program, including numbers,
variable names, operators, keywords, and punctuation like parentheses, braces
and semicolons.

The standard edition of Java comes with several thousand classes you can
import, which can be both exciting and intimidating. You can browse this

You might also like