+ 5
Please help me solve this question in java and where did I go wrong?🤔
import java.util.Scanner; import java.util.InputMismatchException; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); System.out.println(num1/num2); } catch(ArithmeticException ex) { System.out.println("Еrror: division by zero"); } catch(InputMismatchException ex){ System.out.println("Error: wrong value type"); } } }
21 Answers
+ 5
// Copy this code 57.2 Practice No Zeroes
import java.util.Scanner;
import java.util.InputMismatchException;
public class Main
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
/*
1. Еrror: division by zero
2. Error: wrong value type
*/
//your code goes here
System.out.println(num1/num2);
} catch(ArithmeticException ae) {
System.out.println("Error: division by zero");
} catch(InputMismatchException ime){
System.out.println("Error: wrong value type");
}
}
}
+ 5
YIPMONG
Error: division by zero
Here Error has a different language letter not English letter which nobody can see.
So just copy that from the description and paste in code. Code will work.
+ 4
Just try to rewrite output statement your own, instead of copy from description. There may be mismatching in characters farmat.
+ 4
SoloProg
Sir yours worked👍
But how🤔
Thank you so much for the help🤗
+ 3
Try removing try catch error
+ 3
Victor Ay
I'm trying to create an exception so try catch is needed
Is from java code coach
+ 3
What the name of the code coach
+ 3
// Try it
System.out.println( (double) num1/num2 );
+ 3
what error you are having ?
+ 3
Victor Ay
Multiple exceptions
+ 3
it is working fine.
+ 3
Davinder Kumar
but in the code coach it shows me an error
Please check my post for visuals of the error
+ 3
tell me first, what input are you giving ??
+ 3
Most likely the problem is in the first error message the E is a different character than the second.
+ 3
Jayakrishna🇮🇳
I did that but still I got same error
+ 3
catch(ArithmeticException e) {
System.out.print("Error: division by zero");
}
//This working fine for me... Try it once..
edit:
may be actually use as it is in code comments by copy paste, if it not work..
+ 3
Ok Jayakrishna🇮🇳
Thank you so much sir
Let me check it out
+ 2
Davinder Kumar
The input is a 0 division
That is first input is 1 or any number apart from 0 and the second input is 0
+ 1
Davinder Kumar
Let me use playground and send you the link and error in a post
+ 1
SoloProg it didn't work😓