• Top Members
    Reps
    Posts
  • 834 Replies
    2585 Replies
  • 713 Replies
    2021 Replies
  • 331 Replies
    1928 Replies

JOptionPane Input Dialog Problem

Locked
User avatar
Rhez
Member
Posts: 30

JOptionPane Input Dialog Problem

Thu Jan 15, 2015 10:43 am

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
Posts: 1242

Re: JOptionPane Input Dialog Problem

Thu Jan 15, 2015 2:57 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
Posts: 30

Re: JOptionPane Input Dialog Problem

Fri Jan 16, 2015 2:22 am

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
Posts: 30

Re: JOptionPane Input Dialog Problem

Fri Jan 16, 2015 2:57 am

Sorry . Maybe the problem is at my IDE. Not in my code.
Somebody mark this as solved

Locked

Return to “Misc”