Exception Soln
Exception Soln
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);
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);
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 {