Digital Assignment3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

DIGITAL ASSIGNMENT – 3

EXCEPTION HANDLING
NAME : S.LAHARI
REG NO : 21MIS0091
1. Create a program that takes in 10 strings in a string array from the user.
Consider the user is entering the file names as “File.txt”. Compare the
elements of the String array of filenames and print in ascending order. a.
Once the user entered a file name check whether the format is “.xls” or
“.txt” or ”.pdf”. If the user enters the file names with some other types
raise an IIlegalFileTypeInInputException. b. Also, concatenate the string
array elements into a string and find the count of the letter a. If the
count of letter a is greater than 10 throw OverFlownletteraException
and if the count of the letter a is lesser than 3 throw
UnderFlowLetteraException. Implement the required Exception handling
mechanism.
CODE :
import java.util.Scanner;
class IllegalFileTypeInInputException extends Exception{
}
class OverFlownletteraException extends Exception{
}
class UnderFlowLetteraException extends Exception{
}
public class Ass3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n= 10;
String[] s = new String[n];
System.out.println("Enter 10 Strings (file.txt)");
for (int i = 0 ; i<n ; i++){
s[i] = sc.nextLine();
}
for (int i = 0 ; i<s.length ; i++){
for(int j = i+1 ; j<s.length ; j++){
if (s[i].compareTo(s[j])>0){
String temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
System.out.println("String Array in Ascending Order ");
for (String ss: s){
System.out.println(ss);
}
try{
for (int i = 0 ; i <n; i++){
if (!s[i].endsWith(".txt")||!s[i].endsWith(".pdf")||!
s[i].endsWith(".xls")){
throw new IllegalFileTypeInInputException();
}
}
} catch (IllegalFileTypeInInputException e) {
System.out.println("IIllegalFileTypeInInputException");
}
StringBuilder t = new StringBuilder(new String());
for (int i = 0 ; i <n ;i++){
t.append(s[i]);
}
short countC = 0;
for (int i = 0 ; i<n ; i++){
if (t.charAt(i)=='a'||t.charAt(i)=='A'){
countC++;
}
}
System.out.println("Count C = "+ countC);
try{
if (countC>10){
throw new OverFlownletteraException();
} else if (countC < 3) {
throw new UnderFlowLetteraException();
}
} catch (OverFlownletteraException e) {
System.out.println("OverFlownletteraException");
}
catch(UnderFlowLetteraException e){
System.out.println("UnderFlowLetteraException");
}
}
}
OUTPUT :

You might also like