0% found this document useful (0 votes)
13 views4 pages

Exp11 60003190025

Uploaded by

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

Exp11 60003190025

Uploaded by

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

EXPERIMENT 11

Himanshu Harlalka
60003190025

AIM: To implement exceptions in Java

PROBLEM STATEMENT 1:
Write a Java Program to read input from user in an array using
datainputstream and Find out total valid and in-valid integers. (Hint: use
exception handling)

CODE:
import java.io.*;

public class validate {


public static void main(String[] args) throws Exception {
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int valid=0, invalid=0;
int n;
System.out.print("Enter the number of numbers to input: ");
n = Integer.parseInt(in.readLine());
int arr[] = new int[n];
System.out.println("Enter the numbers: ");
for(int i=0; i<n; i++)
{
arr[i] = Integer.parseInt(in.readLine());
}
for(int i=0; i<n; i++)
{
try
{
if(Integer.parseInt(String.valueOf(arr[i]))<=127 &&
Integer.parseInt(String.valueOf(arr[i]))>=-128)
{
System.out.println("Valid Byte");
valid++;
}
else if(Integer.parseInt(String.valueOf(arr[i]))<32767 &&
Integer.parseInt(String.valueOf(arr[i]))>=-32768)
{
System.out.println("Valid Integer");
valid++;
}
else
{
throw new Exception();
}
}
catch(Exception e)
{
System.out.println("Not any Primitive Data Type");
invalid++;

}
}
System.out.println("Valid: "+valid);
System.out.println("Invalid: "+invalid);
}
}
OUTPUT:

Enter the number of numbers to input: 4


Enter the numbers:
23
123456
345
32832
Valid Byte
Not any Primitive Data Type
Valid Integer
Not any Primitive Data Type
Valid: 2
Invalid: 2

PROBLEM STATEMENT 2: Write a Java Program to Create a User Defined


Exception class MarksOutOfBoundsException, If Entered marks of any subject
is greater than 100 or less than 0, and then program should create a user
defined Exception of type MarksOutOfBoundsException and must have a
provision to handle it.

CODE:
import java.util.*;
public class valid_marks {
static Scanner Sc = new Scanner(System.in);
static int marks, seat_no, center_no;
static String name, date;

public static void main(String[] args) throws MarksOutOfBoundsException {


read();
read1();
if(marks>100 || marks<0) {
try {
throw new MarksOutOfBoundsException();
}catch (MarksOutOfBoundsException ab){

}
}
else
display();
}

public static void read() {


System.out.println("Enter name of the student: ");
name = Sc.next();
System.out.println("Enter seat number of student: ");
seat_no = Sc.nextInt();
System.out.println("Enter the date of examination (format dd-mm-yyyy): ");
date = Sc.next();
System.out.println("Enter center number of the exam: ");
center_no = Sc.nextInt();
}

public static void read1() {


System.out.println("Enter marks of the exam: ");
marks = Sc.nextInt();
}

public static void display()


{
System.out.println("NAME \t SEAT NUMBER \t DATE \t CENTER NUMBER \t
MARKS");
System.out.println(name+" \t "+seat_no+"\t "+date+"\t "+center_no+"\t "+marks);
}
}

class MarksOutOfBoundsException extends Exception {


public MarksOutOfBoundsException() {
do {
System.out.println("Marks are invalid \nEnter the details again \n");
valid_marks.read1();
} while(valid_marks.marks>100 || valid_marks.marks<0);
valid_marks.display();

}
}

OUTPUT:
Enter name of the student:
himanshu
Enter seat number of student:
25
Enter the date of examination (format dd-mm-yyyy):
10-10-2020
Enter center number of the exam:
3
Enter marks of the exam:
102
Marks are invalid
Enter the details again

Enter marks of the exam:


23
NAME SEAT NUMBER DATE CENTER NUMBER MARKS
himanshu 25 10-10-2020 3 23

You might also like