This week, we focused on the input function—Scanner class and the looping of Java. The pace is getting faster and faster, so I think it’s really important to read ahead the Java illuminated book.
Scanner class
Scanner class can read byte, short, int, long, float, double, and String data types. It uses java.util package, thus enables users to enter the numbers to the program. Here is the typical example of how to use the Scanner class.
import java.util.Scanner;
Scanner keyboard = new Scanner (System.in);
num = keyboard.nextInt();
By writing 3 statements above, we can use Scanner class in our program to allow user to enter input. There is an exercise that we did in class to calculate the average score for students in the class.

Looping methods
- While:
By using while statement, algorithm within the while code block will continue to be implemented until conditions in parentheses after “while” is not true. - Do-while:
It is similar with while statement. The only difference is that do-while will first perform the calculation once no matter the condition satisfies or not, whereas while statement checks conditions first. - For loop:
For loop simplifies the content in while and do-while loop. Because for loop can be used for definite loops when the number of iterations are known.
Type-Safe
hasNextInt() method can check if users’ input satisfies the requirement. It will returns true if the number entered is an integer. Similarly, there are also hasNextBoolean() and hasNextDouble().
int or boolean or double value, the program won’t stop and display the error; instead, it will continue asking the user to enter a String value until the input is a String value.
Guessing game
I used two loops in this guessing game, while and do-while, also here the random method was introduced.
As shown in line 8 and 16, I firstly imported Random, then typed “Random ran = new Random();”. System will randomly pick a number between 0 to 100;

We also created a program that output estimated height of a male or female child based on height of mother and father.

Java escape sequence
In Java, we have different escape characters to perform different following outputs:
| Escape Sequence | Description |
| \t | Insert a tab |
| \n | Insert a new line |
| \’ | Insert a single quote |
| \” | Insert a double |
| \\ | Insert a backslash |
Implicit casting: when two types of data are compatible, java will automatically convert one data type to another when one type of data is assigned to another.
Explicit casting: In order to promote precision whenever required, we use this syntax: (dataType) (expression) For example,
average = (double) total / count;
Shortcut Arithmetic Operators
Shortcuts and simplify our code and make it looks better.
- (++): increment by 1
- (–): decrement by 1
- Postfix: (var++, var–): use original value in expression, and then do the increment or decrement
- Prefix: (++var, –var): increment or decrement first, then do the operations. e.g.
for ( int i = 0; i <= 100; i++ )
Logical operators
This part is highly related to the logic gates that we learned few weeks ago. It can be used to compare the true or false of statements.

Here is how we perform logical operators in BlueJ.

Some exercises that we did are shown below:



The looping part for java is quite straightforward, because I’ve learned those in c++ and python, so it would be quite easy to understand. However, some of the problems may have various approaches, and it’s difficult to determine which one is the best or the most efficient way, so this is something that I need to improve in the future.
