Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ict4 LG19

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Learning Guide

Grade 10 - ICT 4

Lesson 19 : Reading Data Using the Scanner class

Pre-requisite Concept : Understanding the Building Blocks of Java

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

Getting User Inputs

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:

static Scanner console = new Scanner(System.in);

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)

c. The expression: console.next()


retrieves the next input token as a string; that is, the value of this expression is
the next input string. (Note that is the next input token is a number, this
expression interprets that number as a string.)

d. The expression: console.nextLine()


retrieves the next input as a string until the end of the line; that is, the value of
this expression is the next input line. (Note that this expression also reads the
newline character, but the newline character is not stored as part of the string.)

While scanning for the next input, the expressions console.nextInt(),


console.nextDouble(), and console.next() skip whitespace characters. Whitespace
characters are blanks and certain nonprintable characters, such as newline and tab.

The following Java program shows the effect of the preceding input
statements:

//This program illustrates how input statements work.

import java.util.*;

public class Example;


{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)


{
int feet;
int inches;

System.out.println(“Enter two integers separated by


spaces.”);

feet = console.nextInt();
inches = console.nextInt();

System.out.println(“feet = ” + feet);
System.out.println(“inches = ” + inches);
}
}
Sample Run:

Enter two integers separated by spaces.


23 7
feet = 23
inches = 7

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:

The value of num1 = 8 and the value of num2 = 5.

Reminders

Do the activity and submit it on March 14, 2022.

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)

You might also like