Ict4 LG19
Ict4 LG19
Ict4 LG19
Grade 10 - ICT 4
Time : 3 hours/week
Introduction
This learning guide will discuss on how to read data using the Scanner class.
Objectives
1. Understand the syntax in reading user’s input using the Scanner class.
2. Write a Java application that reads in data, performs calculations on the given
data, and displays the result.
Lesson Proper
To put data into variables from the standard input device, Java provides the class
Scanner. Using this class, we first create an input stream object and associate it with
the standard input device. The following statement accomplishes this:
this statement creates the input stream object console and associates it with the
standard input device. (Note that Scanner is predefined Java class and the preceding
statement creates console to be an object of this class.) The object console reads the
next input as follows:
a. If the next input token can be interpreted as an integer, then the expression:
console.nextInt();
retrieves that integer; that is, the value of this expression is that integer.
b. If the next input token can be interpreted as a floating-point number, then the
expression: console.nextDouble()
retrieves that floating-point number; that is, the value of this expression is that
floating-point number. (Note that an integer can be treated as a floating-point
with 0 decimal part)
The following Java program shows the effect of the preceding input
statements:
import java.util.*;
feet = console.nextInt();
inches = console.nextInt();
System.out.println(“feet = ” + feet);
System.out.println(“inches = ” + inches);
}
}
Sample Run:
Activity 19
Answer the following problem. Write your answers in a one whole sheet of paper.
Don’t forget to write your name, grade level, section and activity number.
Write a Java program that reads the input for num1 and num2 and then displays
the value of num1 and num2, indicating which is num1 and which is num2. For
example, if num1 is 8 and num2 is 5, then the output is:
Reminders
References
1. Computer Advantage: Understanding Structured Programming (Second
Edition), 2009 By Teresita P. Pasadas.et. al.
(Textbook)
2. Developing Useful Computer Applications for Today’s World Fundamentals of
Web Application Development II (2008)
By Dennis Cesar R. Patiño
3. Introduction to Computer by Antonio M. Andres, Sr. (2003)