Page 1 of 1

JOptionPane Input Dialog Problem

Posted: Thu Jan 15, 2015 9:43 am
by Rhez
Because I am a VB.NET Programmer . I don't know what the hell is going on in my code.
I am a college student and we are now on JAVA programming.

Ok so I'm trying to do a JOptionPane and this is my code :
Code: Select all
 Scanner nn = new Scanner (System.in);
        String dlg;
        int n1,n2,x1;
        System.out.print("First Number : ");
        n1 = nn.nextInt();
        System.out.print("Second Number : ");
        n2 = nn.nextInt();
        dlg = JOptionPane.showInputDialog("Enter your Choice :");
When I started to run this. Only the First and Second number was running. I wait for the InputDialogBox to appear but nothings happen.

PS : there is no error at all. And I don't know what is happening.

Re: JOptionPane Input Dialog Problem

Posted: Thu Jan 15, 2015 1:57 pm
by comathi
If this is your whole code, then there are many possible error sources.

First of all, you should make sure to import both the Scanner and the JOptionPane classes. This is done with the following code:
Code: Select all
import java.util.Scanner;
import javax.swing.JOptionPane;
Secondly, your code should be within a class, which has the same name as your filename. For example, if your filename is dialogs.java, you should declare your class like so:
Code: Select all
public class dialogs {

}
Then, inside your class, as you've already done, you can declare your Scanner object:
Code: Select all
static Scanner nn = new Scanner(System.in);
Also, every Java program needs a main class, which you haven't declared here, so add this under the Scanner declaration line:
Code: Select all
public static void main(String[] args) {

}
Inside the main function, you can now add the rest of your code:
Code: Select all
        String dlg;
        int n1, n2, x1;
        
        System.out.print("First Number: ");
        n1 = nn.nextInt();
        
        System.out.print("Second Number: ");
        n2 = nn.nextInt();
        
        dlg = JOptionPane.showInputDialog("Enter your Choice:");
Your entire code should now look like this:
Code: Select all
import java.util.Scanner;
import javax.swing.JOptionPane;

public class dialogs {
    static Scanner nn = new Scanner(System.in);
    
    public static void main(String[] args) {
        String dlg;
        int n1, n2, x1;
        
        System.out.print("First Number: ");
        n1 = nn.nextInt();
        
        System.out.print("Second Number: ");
        n2 = nn.nextInt();
        
        dlg = JOptionPane.showInputDialog("Enter your Choice:");
    }
}
If you compile this with javac and then run it, it should work (does for me) cooll;

Image

Re: JOptionPane Input Dialog Problem

Posted: Fri Jan 16, 2015 1:22 am
by Rhez
No offend
I said there is no error sir.
The only problem is it not popping up as it should. I am using NetBeans as IDE

Re: JOptionPane Input Dialog Problem

Posted: Fri Jan 16, 2015 1:57 am
by Rhez
Sorry . Maybe the problem is at my IDE. Not in my code.
Somebody mark this as solved