IN JAVA Hello, please i'm working on this code and I'm stuck at two try catch blocks: one in the main and one the method
Posted: Sat May 14, 2022 7:48 pm
IN JAVA
Hello, please i'm working on this code and I'm stuck at two try
catch blocks: one in the main and one the method divide.
1 - in the main: the two try catch blocks should one of them try
to catch the error if the user input a different size for the
array. The second try catch block should catch any input mismatch
error if the user enters a string or a character.
2- in the method divide there is two try catch blocks. One
should catch if the second number is 0 because we cannot divide by
0. The second try catch block should catch the error if the user
input a string or a character.
public class TestException {
public static int SIZE = 10;
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new
Scanner(System.in);
int x, y, elements = 0;
System.out.print("Enter 2 integers
to calculate the division "
+ "and the
sum of those integers: ");
x = input.nextInt();
y = input.nextInt();
int[] array = new int[SIZE];
System.out.println("Enter the number
of elements");
elements = input.nextInt();
System.out.println("Enter the " +
array.length + " elements: ");
try {
for (int i = 0; i <
array.length; i++) {
try {
array = input.nextInt();
} catch
(Exception InputMismatchException) {
System.out.println("Exception of type "
+ "InputMismatchException: Wrong
Input!");
break;
}
}
} catch (Exception
ArrayIndexOutOfBounds) {
System.out.println("Exception of type ArrayIndexOutOfBounds "
+ "Exception: Index " + elements + " out of bounds "
+ "for length " + SIZE);
}
}
public static double divide(int n1, int n2)
{
double division = 0;
try {
division = n1 / n2;
} catch (ArithmeticException e) {
System.out.println("Exception of type ArithmeticException: "
+ "Should not be divided by zero");
}
try {
division = n1 / n2;
} catch (InvalidInputException e)
{
System.out.println("Exception of type "
+ "InputMismatchException: Wrong Input!");
}
return division;
}
public static int sum(int n1, int n2) {
return n1 + n2;
}
public static void calculation(int n1, int n2)
{
System.out.println("The sum of " + n1 +
" and " + n2 + " is "
+ sum(n1,
n2));
System.out.println("The division of " +
n1 + " and " + n2 + " is "
+
divide(n1, n2));
}
Hello, please i'm working on this code and I'm stuck at two try
catch blocks: one in the main and one the method divide.
1 - in the main: the two try catch blocks should one of them try
to catch the error if the user input a different size for the
array. The second try catch block should catch any input mismatch
error if the user enters a string or a character.
2- in the method divide there is two try catch blocks. One
should catch if the second number is 0 because we cannot divide by
0. The second try catch block should catch the error if the user
input a string or a character.
public class TestException {
public static int SIZE = 10;
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new
Scanner(System.in);
int x, y, elements = 0;
System.out.print("Enter 2 integers
to calculate the division "
+ "and the
sum of those integers: ");
x = input.nextInt();
y = input.nextInt();
int[] array = new int[SIZE];
System.out.println("Enter the number
of elements");
elements = input.nextInt();
System.out.println("Enter the " +
array.length + " elements: ");
try {
for (int i = 0; i <
array.length; i++) {
try {
array = input.nextInt();
} catch
(Exception InputMismatchException) {
System.out.println("Exception of type "
+ "InputMismatchException: Wrong
Input!");
break;
}
}
} catch (Exception
ArrayIndexOutOfBounds) {
System.out.println("Exception of type ArrayIndexOutOfBounds "
+ "Exception: Index " + elements + " out of bounds "
+ "for length " + SIZE);
}
}
public static double divide(int n1, int n2)
{
double division = 0;
try {
division = n1 / n2;
} catch (ArithmeticException e) {
System.out.println("Exception of type ArithmeticException: "
+ "Should not be divided by zero");
}
try {
division = n1 / n2;
} catch (InvalidInputException e)
{
System.out.println("Exception of type "
+ "InputMismatchException: Wrong Input!");
}
return division;
}
public static int sum(int n1, int n2) {
return n1 + n2;
}
public static void calculation(int n1, int n2)
{
System.out.println("The sum of " + n1 +
" and " + n2 + " is "
+ sum(n1,
n2));
System.out.println("The division of " +
n1 + " and " + n2 + " is "
+
divide(n1, n2));
}