The Unknown
The Unknown

Reputation: 11

java dialog box is not working after writing this code

I was working on java by writing a try and catch code. I wrote in the first number 5 for instance and in the second 0 and it's an error so it will jump on catch, but in catch statement I wrote dialog box but it's not working on this code, but in any code it runs. I tried a lot in finding the problem. This is the code:

public static void main(String[] args) {
    try {
        java.util.Scanner s = new java.util.Scanner(System.in);
        System.out.println("Enter number 1 :");
        int num1 = s.nextInt();
        System.out.println("Enter number 2 :");
        int num2 = s.nextInt();
        int cal = num1 / num2;

    } catch(Exception x){
        javax.swing.JOptionPane.showMessageDialog(null, "something Wrong happend");
    }
}

The question here why dialog box is not working and why it stopped at this dialog box?

Upvotes: 0

Views: 336

Answers (1)

Abra
Abra

Reputation: 20914

The dialog box is working. It is probably hidden behind another window. Try to iterate through all your windows until you find it. Until you find it and close it, your program will be stuck. It will not continue executing until you close the JOptionPane.

For what it's worth, although using a JOptionPane in a java console program works, it is not a good idea, in my opinion, since JOptionPane was not intended to be used in a java console program.

Upvotes: 2

Related Questions