You are on page 1of 27

Opening

Prayer
Learning Objectives
1. Explain what a Scanner
class is.
2. Create a program that uses
a Scanner class with if –
else statements
Input

Scanner
Input
Input
Come to think of it..

Would it better for Java to receive an


input of a value from a user/audience
rather than us, the programmers, to
always put the initial value to our
variables?
Input
Most of the time, we always put the initial value
to our variables like

int Age = 15;

We declare that the Age has a value of 15

But can our audience/users type in their value


instead of us, providing an initial value?
Input

We use the Scanner


class to let Java receive
an input from a user.
Scanner class
Ex. You create a Java program to ask a user
for their age and do something about it based
on that age they have provided.

The ‘Scanner’ helps you get that age from the


user so your program can make decisions
accordingly.
Scanner class

What does the Java


Program look like then
with Scanner?
Constant Syntax in having a Scanner

import.java.util.Scanner;

We are using a Scanner class


to read a value input from a
user
Scanner class

Always declare the Scanner statement at the


top of your Java codes to allow us to enable
Java Program to read an input from the user
Scanner class

In line 5, With Scanner being a class, perform


the Object Instantiation to create an instance
or object of a Scanner class

Scanner scanner
Scanner class

Afterwards, put in new to create a new


instance of a class of Scanner

(System.in) means a default source of input.


Particularly used for reading data by a user
Scanner class

Line 7 means Java will print out Enter your Age, prompting
our user to type in a number (Age).

Line 8, we declare that the value of Age is the value provided


by our user.

nextInt(); means a method in Scanner class used to read an


integer value from a keyboard which is typed in by the user
Scanner class

In line 19, we declare that our scanner should close.

Meaning to release any resources associated with Scanner. Good for


maintaining good memory management and system efficiency

scanner.close(); belongs to the scope of open and close brace of


public static void main(String[] args) { }
A Java
Program
with a
Scanner
class
should
look like
this
Scanner on words and sentences

Scanner can also receive words and sentences, aside


from integers
System.out.print(“Enter a word: “);
String word = scanner.next(); //Reads a single word

System.out.print(“Enter a sentence: “);


scanner.nextLine(); //Reads the word starting from left
String sentence = scanner.nextLine(); //Reads a full line of text
Scanner on a single character only

Scanner can also receive single characters aside from


integers.

charAt(0); read and extract only the first character of


the you have provided.
Scanner on double

Scanner can also receive double values aside from


integers.
Input

Scanner Class

= Used to read an input


from a user
Hands – on Activity 6:
Age Check!

Finding God in All Things


Create a Java program where
you will be using the else – if
statements to check an age of
a person

Finding God in All Things


Create a Java Program and Java class named Age_Check
that checks an Age of a person using else – if statements

Create a variable that will store the value of Age

In your first condition, when Age <= 18 then print out,


You are still a kid!

In your second condition, when Age >= 30 then print out,


You are an adult!

Else, print out, You are a young adult!


Finding God in All Things
HoA5: Age Check!
Submission in Teams: A less than 30 - sec Video

- 2 seconds of you
- 10 seconds of your Java Codes
- 10 seconds of your Terminal Window

Make sure to use your back camera


No cuts in the video

Finding God in All Things


Closing
Prayer

You might also like