Full Stack
🗙
Software Engineering
🗙
Java Comment and Input Scanner
By Max Wong
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
2
What will you learn?
• Multi-line and Single line comment
• Getting user input from keyboard
• Understand String (a data type)
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
Comment
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
4
Multi-line Comments
/*
* The HelloWorld Application Can you figure out the use of it?
*/
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello World!");
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
5
Single-line Comments
// This is a single line comment.
public class HelloWorld {
public static void main(String [] argv) {
[Link]("Hello world!"); // print something
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
6
Usage of Comments
• Programmers leave comments to their colleagues or themselves to
explain the usage, input, output or even “todo” of
program segments
However, a good program can explain itself by its good coding design and structure.
If the code segment is clear enough, there is no need to write a comment.
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
Getting User Input
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
8
User Input from Keyboard
import [Link].*; Needed if you want to use “Scanner”.
public class Score {
public static void main(String [] args)
{
Scanner keyboard = new Scanner([Link]); It doesn’t matter what this means now.
int midterm, finalExam; Just copy it.
double average;
[Link]("Midterm? ");
midterm = [Link](); This reads an integer from the keyboard.
[Link]("Final? ");
finalExam = [Link]();
average = (midterm + finalExam) / 2.0;
[Link] ("Average = " + average);
}
}
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab example_projects/L005_Score
9
Data Type - String
Introduce a new data type here: String
A String is a sequence of characters, its value is wrapped in two
double quotes ("") , just like the [Link]("Hello world!");
in the HelloWorld example.
Different with the data types learnt in the previous lesson,
its first character is in capital letter.
No need to understand why right now, we will learn this in detail in later lessons.
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
10
Read a String from Keyboard
import [Link].*; Outputs:
public class ReadWord { > java ReadWord
public static void main(String [] args) { Hello
Scanner keyboard = new Scanner([Link]); Your input is: Hello
String input = [Link](); > java ReadWord
Good Morning, Peter!
[Link]("Your input is: " + input); Your input is: Good
}
}
Only one single word is read!
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab example_projects/L005_ReadWord
11
Reading a Whole Line from Keyboard
import [Link].*;
public class ReadLine {
public static void main(String [] args) {
Scanner keyboard = new Scanner([Link]); > java ReadLine
Good Morning, Peter!
String input = [Link](); Your input is: Good Morning, Peter!
[Link]("Your input is: " + input);
}
}
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab example_projects/L005_ReadLine
12
Scanner Input Types
Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextShort() Reads a short value from the user
nextInt() Reads an int value from the user
nextLong() Reads a long value from the user
nextFloat() Reads a float value from the user
nextDouble() Reads a double value from the user
next() Reads a word in String value from the user
nextLine() Read a whole line in String value from the user
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
13
What did you learn?
• Multi-line and Single line comment
• Getting user input from keyboard
• Understand String (a data type)
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab