IT112 COMPUTER PROGRAMMING 1
FIRST SEM_FIRST TERM BSIT 1A
Module 3 BufferedReader br=new
BufferedReader(dataIn);
GETTING INPUT FROM THE KEYBOARD
Note
Lesson 1: Using BufferedReader
• BufferedReader – Buffered class
USING BUFFEREDREADER
• dataIn - is the variable to accept when
• Bufferedreader is a java class that reads
accepting input
text from the input stream.
• new BufferedReader (new
• It buffers the characters so that it can
BufferedReader (System.in)); - The
get the efficient reading of characters,
BufferedReader can’t read the
arrays, etc.
InputStream directly; So, we need to use
• It inherits the reader class and makes an adapter like InputStreamReader to
the code efficient since we can read the convert bytes to characters format.
data line-by-line with the readline()
• InputStreamReader - is a bridge from
method.
byte streams to character streams: It
• There are a few pointers we have to reads bytes and decodes them into
keep in mind while working with characters using a specified charset.
bufferedreader class in java. Used to read data from keyboard.
STEPS IN GETTING INPUT • System.in in java means to take input
from the keyboard or user. System. out
1. The first thing to do is to reference the library
in java means to print the output to
we want to use. Add the import line to your new
console.
project.
3. Once we have created a BufferedReader we
import java.io.*;
can use its method readLine() to read one line
Note: of characters at a time from the keyboard and
store it as a String object. When we have the
• import -basically means you're adding a
input data in a String object we can use the
Library/Package full of classes and
various methods available to a String object to
methods that are premade for ease of
manipulate the data.
use.
Now, we will parse the input string to get the
• java.io - is standard for Java
integer value of our age. We are now changing
Input/Output. So in essence you're
the type of string from the variable input to a
importing extra Input/Output
type of integer and store in the variable age.
functionality.
One way to read an integer is to read the input
• (*) - The asterisk on the end means as a String and then use the method parseInt()
you're importing everything in the of the wrapper class Integer to convert the
Java.IO Package. String to an integer.
2. Add this statement inside the try catch block Java Exceptions
or after the main class to be able to use the
• When executing Java code, different
BufferedReader class:
errors can occur: coding errors made by
BufferedReader dataIn = new the programmer, errors due to wrong
BufferedReader(new input, or other unforeseeable things.
InputStreamReader(System.in)); or
InputStreamReader dataIn=new
InputStreamReader(System.in);
IT112 COMPUTER PROGRAMMING 1
FIRST SEM_FIRST TERM BSIT 1A
• When an error occurs, Java will normally Expected Output
stop and generate an error message. Enter number 1: 10
The technical term for this is: Java will
throw an exception (throw an error). Enter number 2: 20
Addition: 30
Java try and catch
Multiplication: 200
• The try statement allows you to define a
block of code to be tested for errors Division: 2
while it is being executed. Lesson 2: Using JOptionPane
• The catch statement allows you to Using JOptionPane
define a block of code to be executed, if
an error occurs in the try block. • Another useful class for accepting user
input, and displaying results, is the
Since data can get corrupted during an input JOptionPane class.
operation the method, readLine() can alert the
user by catching the error and printing the error • This is located in the javax.swing library.
message using System.out.println("Error: " + • The JOptionPane class is used to
error.getMessage());. You have to type it inside provide standard dialog boxes such as
a try-catch block. message dialog box, confirm dialog box
Syntax of Try Catch and input dialog box.
try { • These dialog boxes are used to display
information or get input from the user.
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
STEPS IN GETTING INPUT
}
• The first thing to do is to reference the
library we want to use. Add the import
line to your new project, and your code
window should look like something like
this:
import javax.swing.JOptionPane; or import
javax.swing.*;
• This tells java that we want to use the
JOptionPane class, located in the
javax.swing library. On the program you
Practice for coding: will see a wavy underline, that only
means that we haven't used the
Write a program, using BefferedReader that JOptionPane class yet. It will go away
will ask the user to input two numbers and do once we do.
the following operations.
• To get an input from the user using
• Addition = add the two numbers JOptionPane class we will be using the
• Multiplication = multiply the two numbers showInputDialog method of
JOptionPane. After that, create a
• Division = divide the two numbers. variable that will hold the value of the
IT112 COMPUTER PROGRAMMING 1
FIRST SEM_FIRST TERM BSIT 1A
input given by the user. Your code Joptionpane get numerical
should look like this.
• If you want to get numerical value on
Syntax your program using JOptionPane, you
must use the parsing method as what
String name =
we have discussed on the
JOptionPane.showInputDialog("Enter your
BufferedReader class. You will need to
name: ");
parse the data because JOptionPane
Example…… only accepts string inputs. Check the
sample program below.
• To display the result of the input given
by the user, we will be using the • int
showMessageDialog method of age=Integer.parseInt(JOptionPane.show
JOptionPane. InputDialog(null, "Enter your age: "));
Syntax • double
age=Double.parseDouble(JOptionPane.
JOptionPane.showMessageDialog(null, "Your
showInputDialog(null, "Enter your age:
name is "+name);
"));
Example……
• float age =
Customization Float.parseFloat(JOptionPane.showInpu
tDialog(null, "Enter your age: "));
• You may also customized the message
box displayed above, based from the Example………
output that you wanted to convey such
• In windows-based applications, Java
as the following:
Swing makes it very easy to develop
• Information Message them and it is a very powerful API.
• Plain Message
• JOptionPane being a part of it simplifies
• *Error Message
creating dialog boxes in an easy
• Warning Message
manner.
• Question Message
• It provides standard dialog boxes such
Confirmation dialog
as the input dialog box, confirms dialog
• JOptionPane also have confirmation box and message dialog box. We can
dialog that allows the user to select for create our own custom dialog boxes as
an option, it is the showConfirmDialog well. Since JOptionPane provides
and to get its input, you must use the int standard dialog boxes, it is very useful.
data type because Yes option is
Lesson 3: Using Scanner
equivalent to 0, No option is equivalent
to 1, Cancel option is equivalent to 2 • The Java Scanner class is used to
and if you clicked the window close collect user input.
button “X”, its equivalent value will be -1.
• Scanner is part of the java.util package,
Your code should look like this.
so it can be imported without
Syntax downloading any external libraries.
int option = • Scanner reads text from standard input
JOptionPane.showConfirmDialog(null, "Do you and returns it to a program.
want to continue? ");
In order to work with the Scanner class, you
JOptionPane.showMessageDialog(null, "Your must first import it into your code. There are
choice is "+option); two ways you can do this:
IT112 COMPUTER PROGRAMMING 1
FIRST SEM_FIRST TERM BSIT 1A
• If you only need to work with the • We print out “Product name: ” to the
java.util.Scanner class, you can import console and ask the user to submit a
the Scanner class directly. product name using input.next();.
import java.util.Scanner; • We print out Quantity: to the console
and prompt the user to submit the
• If you are working with other modules in
quantity of the product in stock using
the java.util library, you may want to
input.nextInt();.
import the full library.
• We print out the value of the product
import java.util.*;
name variable and quantity variable to
Java User Input Syntax the console.
• You can collect user input using the Scanner Class Input Types
Scanner class. The Scanner class reads
• In our above example, we collected two
text that a user inserts into the console
types of data from the user: a string and
and sends that text back to a program.
an integer. As mentioned, we had to use
• Scanner is the primary method of different code to collect these types of
collecting Java user input. After you data.
import the Java Scanner class, you can
• Different types of data, like strings and
start to use it to collect user input. Here
integers, are collected using separate
is the syntax for the Java Scanner class:
methods. So, to collect a boolean, you’ll
Scanner input = new Scanner(System.in); use different code than you would to
collect a float.
int number = input.nextInt();
• If you insert the wrong input type, your
Scanner program example
program will raise an
• For instance, suppose we are building InputMismatchException. For example,
an application for a local computer store if you try to insert a double into a field
that keeps track of their inventory. The that collects Booleans, your program will
manager asked us to create a simple raise an exception
program that she can use to add items
to the shop’s inventory list.
• The manager wants to be able to input
two values: the name of the item and its
quantity.
Let’s break down our code step-by-step.
• We import the Scanner library into our
code so that we can receive user input.
• We declare a class called
JavaScannerExample that stores the
code for our program.
• We initialize the Scanner class using
Scanner input = new
Scanner(System.in); The input Java
variable stores our initialized scanner.