Assessment 3
Name: Khushal Chaudhary UID:24BCS10033
Branch: BE – CSE Section:24BCS612-A
Semester: 4 Date:12/3/26
Subject: OOP’s Using Java Subject Code: 24CSH-207
Aim:
a) To write a Java program that converts a given string S into an integer and
handles invalid input by displaying [Link] when
the string contains non-numeric characters.
b) To write a Java program that reads two integers A and B, performs A / B, and
handles exceptions such as division by zero and invalid input using appropriate
exception handling.
Problem 1
Create Write a Java program that reads two inputs A and B and performs integer
division A / B. The program must properly handle the following exceptions: If
B = 0, print [Link]: / by zero If the input contains non-
integer values, print [Link] If no exception occurs,
print the result of A / B. Use appropriate exception handling mechanisms in Java
to prevent program termination.
Program Code:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT.
Your class should be named Solution. */
Scanner o=new Scanner([Link]);
try{
int a=[Link]();
int b=[Link]();
[Link](a/b);
}
catch(ArithmeticException e){
[Link]("[Link]: / by zero");
}
catch(InputMismatchException e){
[Link]("[Link]");
}
}
}
Output
Learning Outcomes:
Understand how to convert a string to integer in Java using [Link]().
Learn how NumberFormatException occurs during invalid type conversion.
Apply exception handling to manage runtime errors in Java programs.
Problem 2
You are given a string S. Your task is to convert the string into an integer. If the
string contains non-numeric characters and cannot be converted into an integer,
print: [Link] Otherwise print the integer value.
Program Code:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT.
Your class should be named Solution. */
Scanner o=new Scanner([Link]);
String s=[Link]();
try{
int n=[Link](s);
[Link](n);
}
catch(NumberFormatException e){
[Link]("[Link]");
}
}
}
Output:
Learning Outcomes
Understand the use of try–catch blocks for handling runtime exceptions.
Learn to handle ArithmeticException and InputMismatchException in Java.
Develop programs that prevent abnormal termination using exception handling.