Next you have to create a Scanner: Scanner in = new Scanner(System.in) Import statements can’t be inside a class definition.īy convention, they are usually at the beginning of the file.
Compiling java cmd two classes one text code#
Using an import statement makes your code unambiguous. It’s necessary because there might be another class named Scanner in another package. This import statement tells the compiler that when you say Scanner, you mean the one defined in java.util. Scanner is provided by java.util, which is a package that contains classes so useful they are called “utility classes”.īefore you can use Scanner, you have to import it like this: import These methods are not easy to use fortunately, Java provides other classes that make it easier to handle common input tasks.įor example, Scanner is a class that provides methods for inputting words, numbers, and other data. The System class also provides the special value System.in, which is an InputStream that provides methods for reading input from the keyboard.
These files are part of the Java library, which is an extensive collection of classes you can use in your programs. In this example the address is 685d72cd, but if you run the same code you might get something different.Īs shown in Figure 3.1, System is defined in a file called System.java, and PrintStream is defined in PrintStream.java. The address of a value is its location in the computer’s memory, which might be different on different computers. The numbers and letters after the sign are the address of System.out, represented as a hexadecimal (base 16) number. The result is: output indicates that System.out is a PrintStream, which is defined in a package called java.io.Ī package is a collection of related classes java.io contains classes for “I/O” which stands for input and output. In fact, we can use to display the value of System.out: (System.out) It also provides System.out, which is a special value that provides methods for displaying output, including println. System is a class that provides methods related to the “system” or environment where programs run.
We have been using for a while, but you might not have thought about what it means.
Compiling java cmd two classes one text how to#
This chapter will show you how to read input from the keyboard, use that input to calculate a result, and then format that result for output. The programs we’ve looked at so far simply display messages, which doesn’t involve a lot of real computation.