Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

Input and Output

Many of the programs that we use in our daily life have a user interface. There are buttons to click, fields to fill in, and rows that scroll infinitely. Later in the semester we will create these components, but for now we’ll use the console to input and output information. When you click Run, your program will run line by line, performing calculations. One of these lines might expect input from the user. The user will then type something into the console/terminal and the program will use that data to calculate some information. Once the calculations are done, we can print the results back to the user and this information will show up on the console.

To be able to get information from the user, we need to use a Scanner object. We’ll learn what objects are in the future, but for now you can think of it as a feature of Java. You first want to tell your program that you want to be able to use Scanners in your code, so you will import the Scanner class. This is done on the first few lines of the program.

import java.util.Scanner

Sometimes you will see the following message:

/usercode/Main.java:5: error: cannot find symbol
    Scanner myScanner = new Scanner(System.in);
    ^
  symbol:   class Scanner
  location: class Main
/usercode/Main.java:5: error: cannot find symbol
    Scanner myScanner = new Scanner(System.in);
                            ^
  symbol:   class Scanner
  location: class Main
2 errors

This looks scary but all is says is that on line 5 you are attempting to create a Scanner object, but you haven’t imported the class. (We will get into this later, but objects are created from classes.)

After importing, you may create a new Scanner:

Scanner myScanner = new Scanner(System.in);

In this case, we created a variable called myScanner, but you give it any descriptive name that you want. The following is also valid, even though the names might not make much sense. Note, you should avoid using names that do not make sense. 🙂

Scanner bob = new Scanner(System.in);
Scanner scannerThing = new Scanner(System.in);
Scanner sc = new Scanner(System.in);

You may prompt the user for input by using several statements.

nextInt() will expect the user to type an integer (a whole number).

next() will take anything the user types and give it to you as a String.

nextFloat() will expect the user to type a decimal.

Output

You can also display information on the screen. This is a bit easier than scanning. The statement is:

System.out.println("some message");

If you’re coming from other languages, this print statement might seem a bit too long. There are reasons for this and you’ll get used to it in due time. Either way, the message that you put between the parenthesis will be displayed to the user. This message can be combined with other numbers or other print statements.


1 System.out.println("my age is : " + 15);            // see note below about the +
2 System.out.println("I live in Hong Kong");          // displays a String
3 System.out.println(9999999);                        // displays a number
4 System.out.println("Column1\tColumn\tColumn3");     // displays column headings

These are all valid statements.

The + operator
The + sign in line 1 above does not denote arithmetic addition, but string concatenation. We will get into this in detail later, but just be aware that the meaning of the + operator depends on the context, namely, what kind of operands are being used. If the two operands are numbers, then the + operator denotes arithmetic addition. If one of the operands is a String, the + operator denotes String concatenation. In the example above, the number 15 is converted to the String “15”, and is then concatenated (added on) to the other String to become, “my age is: 15”.

Escape Sequences
There are a number of characters that can be displayed on the screen that require using an escape sequence. For example, in order to display a tab character as in line 4 above, you specify the tab character as \t. There are others.

Java API
Here is a short video about the Java8 API, and how to find more information about the methods in the String class, and the Scanner class.