0% found this document useful (0 votes)
98 views26 pages

CC102 Lesson 5 Bsit - PPT Java Packagesinput and Exception

This document discusses Java packages, parsing, and exception handling in Java. It covers: - Using packages to prevent naming conflicts and control access - Importing specific packages - Parsing user input using BufferReader and InputStreamReader - Exception handling using try, catch, and finally blocks to handle runtime errors

Uploaded by

Bonjovie Fausto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views26 pages

CC102 Lesson 5 Bsit - PPT Java Packagesinput and Exception

This document discusses Java packages, parsing, and exception handling in Java. It covers: - Using packages to prevent naming conflicts and control access - Importing specific packages - Parsing user input using BufferReader and InputStreamReader - Exception handling using try, catch, and finally blocks to handle runtime errors

Uploaded by

Bonjovie Fausto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Java Packages,

Parsing and
Exception handling

CC101 - INTRODUCTION TO COMPUTING


LESSON V – Java Packages, Parsing and Exception handling

LEARNING OUTCOMES:
At the end of the session, the students should be
able to:
• Design, implement, test and debug a program in
JAVA that uses each of the following fundamental
programming construct: basic computation, simple
I/O and basic error handling
• able to import specific packages
• apply different parsing techniques
• produce an error handling functionality on our
program
CC102 - FUNDAMENTALS OF PROGRAMMING
2
LESSON V – Java Packages, Parsing and Exception handling

Java Packages
• Packages are used in Java in order to prevent naming
conflicts, to control access, to make searching/locating and
usage of classes, interfaces, enumerations and annotations
easier, etc.

• A Package can be defined as a grouping of related


types(classes, interfaces, enumerations and annotations )
providing access protection and name space management.
Some of the existing packages in Java are:
java.lang - bundles the fundamental classes
java.io - classes for input , output functions are bundled in
this package
CC102 - FUNDAMENTALS OF PROGRAMMING
3
LESSON V – Java Packages, Parsing and Exception handling

importing Packages
• These are the basic packages we need for basic input
using bufferreader class with exception handling.

import java.io.*

Or get the specific packages

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

CC102 - FUNDAMENTALS OF PROGRAMMING


4
LESSON V – Java Packages, Parsing and Exception handling

BufferReader, InputStreamReader
and Parsing
• In Java, there are multiple ways to obtain user input
from the keyboard. One of the objects that could
get the job done is a Buffered Reader using
the readline method, along with an Input Stream
Reader.
• the other method is using Scanner Class in Java.
Scanner is a class in java.util package used for
obtaining the input of the primitive types like int,
double etc.
• Parsing is converting the input from string to
desired datatype.
CC102 - FUNDAMENTALS OF PROGRAMMING 5
LESSON V – Java Packages, Parsing and Exception handling

BufferReader and InputStreamReader


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedReaderExample {
public static void main(String[]args)throws IOException{
InputStreamReader ISR = new InputStreamReader(System.in);
BufferedReader BR = new BufferedReader(ISR);
System.out.println("What's your name?");
String userInput = BR.readLine(); //program waits user input here
System.out.println("Your name is : "+userInput);
BR.close();
ISR.close();
}
}
CC102 - FUNDAMENTALS OF PROGRAMMING 6
LESSON V – Java Packages, Parsing and Exception handling

BufferReader and InputStreamReader


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedReaderExample {
public static void main(String[]args)throws IOException{
BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What's your name?");
String userInput = BR.readLine(); //program waits user input here
System.out.println("Your name is : "+userInput);
}
}
//Shorter version of BufferReader and InputStreamReader Line
CC102 - FUNDAMENTALS OF PROGRAMMING 7
LESSON V – Java Packages, Parsing and Exception handling

BufferReader, InputStreamReader w/
import java.io.*; Parsing
public class InputStringtoInt{
public static void main(String[]args)throws IOException{
String StrNumericInput="";
int UserNumericInput=0;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please Enter a number: ");
StrNumericInput = in.readLine();
UserNumericInput= Integer.parseInt(StrNumericInput);
System.out.println(UserNumericInput+" is accepted");
}
}
CC102 - FUNDAMENTALS OF PROGRAMMING 8
LESSON V – Java Packages, Parsing and Exception handling

Exception handling
• The Exception Tree in Java
• Checked Exceptions and Runtime Exceptions
• Standard Exceptions

CC102 - FUNDAMENTALS OF PROGRAMMING


9
LESSON V – Java Packages, Parsing and Exception handling

Understanding Exception in JAVA


• The Exception Tree in Java
• Checked Exceptions and Runtime Exceptions
• Standard Exceptions

CC102 - FUNDAMENTALS OF PROGRAMMING


10
LESSON V – Java Packages, Parsing and Exception handling

The Exception Tree in Java

CC102 - FUNDAMENTALS OF PROGRAMMING


11
LESSON V – Java Packages, Parsing and Exception handling

Checked Exceptions and Runtime


Exceptions
• Handling the exception: catch the exception and
deal with it in the code
• Checked Exceptions:
◦ instances of the Exception class, excluding the
RuntimeException subtree
◦ required to handle
• Runtime Exceptions:
◦ occur due to program bugs
◦ not required to provide code for these exceptions
CC102 - FUNDAMENTALS OF PROGRAMMING 12
LESSON V – Java Packages, Parsing and Exception handling

Basics of Exception Handling


• Using the try and catch Blocks
• Using the finally Block
• Using Multiple catch Blocks

CC102 - FUNDAMENTALS OF PROGRAMMING 13


LESSON V – Java Packages, Parsing and Exception handling

Basics of Exception Handling


• Using the try and catch Blocks
The try block is where line of code with possible run
time error upon execution must placed.
The catch block and its parameter with be triggered
if there are run time errors encountered on the try
block.

CC102 - FUNDAMENTALS OF PROGRAMMING 14


LESSON V – Java Packages, Parsing and Exception handling

Basics of Exception Handling


• Using the finally Block
The finally block will be executed if there are no
errors encountered on the try block or other
acceptable output can be executed based from initial
values of variables before a run time error was
encountered.

CC102 - FUNDAMENTALS OF PROGRAMMING 15


LESSON V – Java Packages, Parsing and Exception handling

Using Multiple catch Blocks


• Only one relevant catch block, encountered
first by the execution control, will be executed
for a given exception.
• The more specific catch block must precede
the less specific catch block. Violation of this
rule will generate a compiler error.

CC102 - FUNDAMENTALS OF PROGRAMMING 16


LESSON V – Java Packages, Parsing and Exception handling

Exception Rules
• If an exception is not caught by any catch block:
1. If there is no finally block, the execution stops
right at the point of exception and returns to the
calling method
2. If there is a finally block, the execution jumps
from the point of exception to the finally block,
and returns to the calling method after the
finally block.

CC102 - FUNDAMENTALS OF PROGRAMMING 17


LESSON V – Java Packages, Parsing and Exception handling

Exception Rules
• If the exception is caught by a catch block:
1. If there is a finally block, the execution jumps
after executing the catch block to the finally block,
and continues until the end of the method.
2. If there is no finally block, the execution jumps
after executing the catch block to the first
statement immediately after the last catch block
and continues to the end of the method.

CC102 - FUNDAMENTALS OF PROGRAMMING 18


LESSON V – Java Packages, Parsing and Exception handling

import java.io.*;
public class InputStringtoInt{
Using the try and
public static void main(String[]args){ catch Blocks
String StrNumericInput="";
int UserNumericInput=0;
BufferedReader UserInput = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Please Enter a number: ");
StrNumericInput = UserInput.readLine();
UserNumericInput= Integer.parseInt(StrNumericInput);
System.out.println(UserNumericInput+" is accepted");
}
catch(Exception E){
System.out.println("Invalid Input");
}
}
}
CC102 - FUNDAMENTALS OF PROGRAMMING 19
LESSON V – Java Packages, Parsing and Exception handling

import java.io.*;
public class InputStringtoInt{
with finally Block
public static void main(String[]args){
String StrNumericInput="";
int UserNumericInput=0;
BufferedReader UserInput = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Please Enter a number: ");
StrNumericInput = UserInput.readLine();
UserNumericInput= Integer.parseInt(StrNumericInput);
}
catch(Exception E){
System.out.println("Invalid Input");
}
finally{
System.out.println(UserNumericInput+" is accepted");
}
}
}
CC102 - FUNDAMENTALS OF PROGRAMMING 20
LESSON V – Java Packages, Parsing and Exception handling

Using
Multiple
catch
Blocks
example

CC102 - FUNDAMENTALS OF PROGRAMMING 21


LESSON V – Java Packages, Parsing and Exception handling

MACHINE PROBLEM 1 + Presentation


• Partner Programming (2 members per group)
• Make a simple Arithmetic Calculator in JAVA.
• your program must accept 2 floating point
numbers.
• Your program must compute all the arithmetic
operators including modulo.
• If there is a math error especially for DIVISION and
a try-catch-finally exception must be applied.
• All output must be on the finally block

CC102 - FUNDAMENTALS OF PROGRAMMING 22


LESSON V – Java Packages, Parsing and Exception handling

MACHINE PROBLEM 1 + Presentation


Sample output1: correct inputs
Please your 1st number:5
Please your 2nd number:2
5 + 2 = 7
5 - 2 = 3
5 x 2 = 10
5 ÷ 2 = 2
5 % 2 = 1

CC102 - FUNDAMENTALS OF PROGRAMMING 23


LESSON V – Java Packages, Parsing and Exception handling

MACHINE PROBLEM 1 + Presentation


Sample output2: alphanumeric input
Please your 1st number:5
Please your 2nd number:a
input is not an integer
5 + 0 = 0
5 - 0 = 0
5 x 0 = 0
5 ÷ 0 = 0
5 % 0 = 0

CC102 - FUNDAMENTALS OF PROGRAMMING 24


LESSON V – Java Packages, Parsing and Exception handling

MACHINE PROBLEM 1 + Presentation


Sample output3: divide by 0 catch error
Please your 1st number:5
Please your 2nd number:0
Divide by Zero Error
5 + 0 = 0
5 - 0 = 0
5 x 0 = 0
5 ÷ 0 = 0
5 % 0 = 0

CC102 - FUNDAMENTALS OF PROGRAMMING 25


LESSON V – Java Packages, Parsing and Exception handling

MACHINE PROBLEM 1 + Presentation


Sample output: non integer input
Please your 1st number:123421342
Please your 2nd number:3.3f
input is not an integer
123421342 + 0 = 0
123421342 - 0 = 0
123421342 x 0 = 0
123421342 ÷ 0 = 0
123421342 % 0 = 0

CC102 - FUNDAMENTALS OF PROGRAMMING 26

You might also like