JOptionPane Input Dialog Problem

Questions/Tutorials for any programming language not covered in other sections.
4 posts Page 1 of 1
Contributors
User avatar
Rhez
Member
Member
Posts: 30
Joined: Sat Jan 04, 2014 7:24 am

JOptionPane Input Dialog Problem
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.
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

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
User avatar
Rhez
Member
Member
Posts: 30
Joined: Sat Jan 04, 2014 7:24 am

Re: JOptionPane Input Dialog Problem
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
User avatar
Rhez
Member
Member
Posts: 30
Joined: Sat Jan 04, 2014 7:24 am

Re: JOptionPane Input Dialog Problem
Rhez
Sorry . Maybe the problem is at my IDE. Not in my code.
Somebody mark this as solved
4 posts Page 1 of 1
Return to “Misc”