MODULE 3: Lesson 1-3 Flashcards

1
Q

USING BUFFEREDREADER

• Buffered reader is a Java class that reads text from the input stream.
• It buffers the characters to get an efficient reading of characters, arrays, etc.
• It inherits the reader class and makes the code efficient since we can read the
data line-by-line with the readline() method.
• We must remember a few pointers while working with the buffered reader
class in Java.

A

STEPS IN GETTING INPUT:
Step
1: The first thing to do is to reference the library we want to use. Add the import
line to your new project.

import java.io.*;

Note:
• import -basically means adding a Library/Package full of classes and
methods that are premade for ease of use.
• java.io - is standard for Java Input/Output. So, in essence, you’re importing
extra Input/Output functionality.
• (*) - The asterisk on the end means you’re importing everything in the Java.IO
Package.

Step 2: Add this statement inside the try catch block or after
the main class to be able to use the BufferedReader

class:
BufferedReader dataIn = new BufferedReader(new
InputStreamReader(System.in));
or
InputStreamReader dataIn=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(dataIn);

Note:
• BufferedReader – Buffered class
• dataIn - is the variable to accept when accepting input
• new BufferedReader (new BufferedReader (System.in)); - The BufferedReader
can’t read the InputStream directly; So, we need to use an adapter like
InputStreamReader to convert bytes to characters format.
• InputStreamReader - is a bridge from byte streams to character streams: It
reads bytes and decodes them into characters using a specified charset. Used
to read data from keyboard.
• System.in in java means to take input from the keyboard or user. System. out in
java means to print the output to console.

  1. Once we have created a BufferedReader we can use its method readLine()
    to read one line of characters at a time from the keyboard and store it as a
    String object. When we have the input data in a String object we can use the
    various methods available to a String object to manipulate the data.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

USING JOptionPane:

• Another useful class for accepting user input, and displaying results, is the
JOptionPane class.
• This is located in the javax.swing library.
• The JOptionPane class is used to provide standard dialog boxes such as
message dialog box, confirm dialog box and input dialog box.
• These dialog boxes are used to display information or get input from the user.

A

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.

import javax.swing.JOptionPane; or import javax.swing.*;

You can do various Dialogues like:

String name = JOptionPane.showInputDialog(“Enter your name: “);

JOptionPane.showMessageDialog(null, “Your name is “+name);

JOptionPane.showMessageDialog(null, “Your choice is “+option);

• You may also customized the message box displayed above, based from the
output that you wanted to convey such as the following:
*Information Message
* Plain Message
* Error Message
*Warning Message
*Question Message
*Confirming Message

JOptionpane can also get numericals

• In windows-based applications, Java Swing makes it very easy to develop them
and it is a very powerful API.
• JOptionPane being a part of it simplifies creating dialog boxes in an easy
manner.
• It provides standard dialog boxes such as the input dialog box, confirms dialog
box and message dialog box. We can create our own custom dialog boxes as
well. Since JOptionPane provides standard dialog boxes, it is very useful.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

USING SCANNER:

• The Java Scanner class is used to collect user input.
• Scanner is part of the java.util package, so it can be imported without
downloading any external libraries.
• Scanner reads text from standard input and returns it to a program.

A

STEPS ON GETTING INPUT:

In order to work with the Scanner class, you must first import it into your code.
There are two ways you can do this:
• If you only need to work with the java.util.Scanner class, you can
import the
Scanner class directly.

import java.util.Scanner;

• If you are working with other modules in the java.util library, you may want to
import the full library.

import java.util.*;

• You can collect user input using the Scanner class. The Scanner class reads
text that a user inserts into the console and sends that text back to a program.
• Scanner is the primary method of collecting Java user input. After you import
the Java Scanner class, you can start to use it to collect user input. Here is the
syntax for the Java Scanner class:

Scanner input = new Scanner(System.in);
int number = input.nextInt();

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.
• We print out “Product name: ” to the console and ask the user to submit a product name using
input.next();.
• We print out Quantity: to the console and prompt the user to submit the quantity of the product
in stock using input.nextInt();.
• We print out the value of the product name variable and quantity variable to the console.

In our above example, we collected two types of data from the user: a string
and an integer. As mentioned, we had to use different code to collect these
types of data.
• Different types of data, like strings and integers, are collected using separate
methods. So, to collect a boolean, you’ll use different code than you would to
collect a float.
• If you insert the wrong input type, your program will raise an
InputMismatchException. For example, if you try to insert a double into a field
that collects Booleans, your program will raise an exception.

Note: To collect user input in java using scannee class you need to Type Next then put the data type and then end with a () .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Java Exception and Java try and catch

A

• When executing Java code, different errors can occur coding errors made by
the programmer, errors due to wrong input, or other unforeseeable things.
• When an error occurs, Java will normally stop and generate an error message.
The technical term for this is: Java will throw an exception (throw an error).

Java try and catch:
• The try statement allows you to define a block of code to be tested for errors
while it is being executed.
• The catch statement allows you to define a block of code to be executed, if an
error occurs in the try block.

Syntax of Try Catch
try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly