Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views2 pages

Assignment 06

The document contains a Java program that allows users to perform two operations: division and accessing an array element. It includes error handling for invalid inputs such as number format exceptions, division by zero, and array index out of bounds. The program prompts the user for input and displays the results accordingly.

Uploaded by

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

Assignment 06

The document contains a Java program that allows users to perform two operations: division and accessing an array element. It includes error handling for invalid inputs such as number format exceptions, division by zero, and array index out of bounds. The program prompts the user for input and displays the results accordingly.

Uploaded by

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

Name - Dhruv Sangwan

Class - IT-B
RollNo - 8212

Assignment - 4

Code -

import java.util.Scanner;

public class assn6 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Choose an operation: \n1. Division \n2. Access Array
Element");
int choice = sc.nextInt();

switch (choice) {
case 1:
try {
System.out.println("Enter Num1:");
String num1Str = sc.next();
System.out.println("Enter Num2:");
String num2Str = sc.next();

int num1 = Integer.parseInt(num1Str);


int num2 = Integer.parseInt(num2Str);

int result = num1 / num2;


System.out.println("Result of division: " + result);
} catch (NumberFormatException e) {
System.out.println("Number Format Exception: Please enter valid
integers.");
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception: Division by zero is
not allowed.");
}
break;

case 2:
try {
int[] arr = {1, 2, 3, 4, 5};
System.out.println("Enter index to access:");
int index = sc.nextInt();
System.out.println("Array element at index " + index + ": " +
arr[index]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index Out of Bounds Exception:
Invalid array index.");
}
break;

default:
System.out.println("Invalid choice! Please choose 1 for Division or
2 for Array Access.");
break;
}
}
}
I/O -

Choose an operation:
1. Division
2. Access Array Element
1
Enter Num1:
12
Enter Num2:
3
Result of division: 4

Process finished with exit code 0

You might also like