0% found this document useful (0 votes)
10 views8 pages

Exception Soln

The document presents multiple Java solutions for exception handling, demonstrating various scenarios such as user input validation, array index management, and product validation. Each solution includes try-catch blocks to handle potential exceptions and provides error messages for invalid inputs. The examples cover a range of topics including sphere volume calculation, sales data management, and book addition validation.

Uploaded by

jetblackaqua957
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)
10 views8 pages

Exception Soln

The document presents multiple Java solutions for exception handling, demonstrating various scenarios such as user input validation, array index management, and product validation. Each solution includes try-catch blocks to handle potential exceptions and provides error messages for invalid inputs. The examples cover a range of topics including sphere volume calculation, sales data management, and book addition validation.

Uploaded by

jetblackaqua957
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/ 8

EXCEPTION HANDLING SOLUTIONS

SOLUTIONS 1

import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
try{
double r= sc.nextDouble();
if(r<=1.0||r>=25.0)
{
System.out.println("Error: Invalid input");
}
else
{
calculateSphereVolume(r);
}
}
catch(Exception e)
{
System.out.println("Error: Invalid input");
}
}
private static void calculateSphereVolume(double radius)
{
System.out.printf("%.2f",1.33*3.14*radius*radius*radius);
}

SOLUTION 2

import java.util.InputMismatchException;
import java.util.Scanner;
class UserInputValidation
{
public static void main(String[] args)
{Scanner sc=new Scanner(System.in);
try
{
int aa=sc.nextInt();
System.out.println("You entered: "+aa);
}
catch(Exception e)
{
System.out.println("Error: Please enter a valid input");
}
finally{
System.out.println("Scanner closed in the finally block");
}
}
}

SOLUTION 3

import java.util.*;
class exceptionhandling
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
try
{
int d=Integer.parseInt(sc.nextLine());
if(d<1||d>30)
{
System.out.println("Error: Number of days sales must be between 1
and 30");
return;
}
try
{
int[] sales =new int[d];
for(int i=0;i<d;i++)
{
sales[i]=Integer.parseInt(sc.nextLine());
}
try
{
int k=Integer.parseInt(sc.nextLine());
if(k<0||k>=d)
{
System.out.println("Error: Invalid index.please provide a
valid day number");
return;
}
System.out.println(sales[k]);
try
{
int s=Integer.parseInt(sc.nextLine());
sales[k]=s;
System.out.println(sales[k]);
}
catch(Exception e)
{
System.out.println("Error: Please enter valid integer sales
amounts");
}
}
catch(Exception e)
{
System.out.println("Error: Please enter valid integer sales
amounts");
}
}
catch(Exception e)
{
System.out.println("Error: Please enter valid integer sales
amounts");
}
}
catch(Exception e)
{
System.out.println("Error: Please enter valid integer sales amounts");
}
}
}

SOLUTION 4

import java.util.*;
class idmanagement
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
try
{
int number=Integer.parseInt(sc.nextLine());
if(number<1||number>500)
{
System.out.println("Error: Number of user IDs must be between 1 and
500");
return;
}
try
{
int[] userid=new int[number];
for(int k=0;k<number;k++)
{
userid[k]=Integer.parseInt(sc.nextLine());
}
try
{
int i=Integer.parseInt(sc.nextLine());
if (i<0||i>number)
{
System.out.println("Error: Invalid index. Please select a
valid index from the list");
return;
}
System.out.println(userid[i]);
try
{
int s=Integer.parseInt(sc.nextLine());
userid[i]=s;
System.out.println(userid[i]);
}
catch(NumberFormatException nfe)
{
System.out.println("Error: Please enter valid interger
values for user IDs");
}
}
catch(NumberFormatException nfe)
{
System.out.println("Error: Please enter valid integer values
for user IDs");
}
}
catch(NumberFormatException nfe)
{
System.out.println("Error: Please enter valid integer values for
user IDs");
}
}
catch(NumberFormatException nfe)
{
System.out.println("Error: Please enter valid interger value for user
IDs");
}
}
}

SOLUTION 5
class Product
{
String na;double pr;int quan;
public Product(String name, double price, int quantity)
{
this.na=name;
this.pr=price;
this.quan=quantity;
}
public static boolean isValidProduct(String name, double price, int quantity)
{
try
{
int len=name.length();
if(len>25)
{
throw new IllegalArgumentException("Product name should be 25
characters or less");
}
if(price<=0)
{
throw new IllegalArgumentException("Price should be a positive
value");
}
if(quantity<0)
{
throw new IllegalArgumentException("Quantity should be a
non-negative integer");
}
return true;
}
catch(IllegalArgumentException ial)
{
System.out.println(ial.getMessage());
return false;
}
}
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

String name1 = scanner.nextLine();


double price1 = scanner.nextDouble();
int quantity1 = scanner.nextInt();

scanner.nextLine(); // Consume the newline character

String name2 = scanner.nextLine();


double price2 = scanner.nextDouble();
int quantity2 = scanner.nextInt();

Product pro1 = new Product(name1, price1, quantity1);


Product pro2 = new Product(name2, price2, quantity2);

if (pro1.isValidProduct(name1, price1, quantity1)) {


System.out.println("Product 1 details are valid");
} else {
System.out.println("Product 1 details are not valid");
}

if (pro2.isValidProduct(name2, price2, quantity2)) {


System.out.println("Product 2 details are valid");
} else {
System.out.println("Product 2 details are not valid");
}

scanner.close();
}
}

SOLUTION 6

class Book
{
private String tit; private String au; private int yearr;
Book(String aa,String bb,int cc)
{
this.tit=aa;
this.au=bb;
this.yearr=cc;
}
void addBook()
{ boolean a=true;
if(tit.length()==0)
{a=false;
throw new IllegalArgumentException("Invalid title: Title should not be
empty");
}
if(au.length()==0)
{a=false;
throw new IllegalArgumentException("Invalid author: Author should not
be empty");
}
if(yearr<0)
{a=false;
throw new IllegalArgumentException("Invalid publication year:
Publication year should be a positive integer");
}
if(a==true)
{
System.out.println("Book added successfully");
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

String title = scanner.nextLine();


String author = scanner.nextLine();
int publicationYear = scanner.nextInt();

try {
Book book = new Book(title, author, publicationYear);
book.addBook();
} catch (IllegalArgumentException e) {
System.out.print(e.getMessage());
}

scanner.close();
}
}

SOLUTION 7

import java.util.Scanner;
public class Main {

public static void main(String[] args)


{
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
int[] arr= new int[N];
for(int i=0;i<N;i++)
{
arr[i]=sc.nextInt();
}System.out.println();
try
{
int M=sc.nextInt();
if(M<0||M>N)
{
throw new ArrayIndexOutOfBoundsException("Error: Array index out of
bounds");
}
else
{
System.out.println("Element at index "+M+": "+arr[M]);
}
}
catch (ArrayIndexOutOfBoundsException aioobe)
{
System.out.println(aioobe.getMessage());
}
}}

[USER DEFIENED SOLUTUONS]


SOLUTION 8

You might also like