I no the problem is not with the program its with my pc. I am able to compile this program fine on my school's pc but i can't on my laptop.

The program is

public class SwingIO
{
    public static void main(String[] args)
    {
        /*
           (To contrast this sample with the ConsoleIO program, note
           that no "throws IOException" clause is required above in
           the declaration of the method: main. Note also that the
           swing input routines do not require declarations analagous
           to those of InputStreamReader and BufferedReader objects
           of the ConsoleIO program.)
        */

        /*
           Following are the input variables of this sample program.
           Note that numbers must be read in as Strings and then
           converted.
        */
        String name;
        String numberString; // Each number will be read here in turn

        /*
           Numeric inputs will be converted to ints and placed in
           these two variables.
        */
        int number1, number2;

        /*
           Following produces an input window to pop us with the prompt
           message shown in the parameter. Whatever the user enters is
           stored in the name variable.
        */
        name = JOptionPane.showInputDialog("What is your first name?");

        /*
           Read and convert the first integer. Note: the program will
           crash in the conversion process if a value entered for either
           the first or second integer are not valid integer numbers.
        */
        numberString = JOptionPane.showInputDialog(
            "Enter first integer value:");
        number1 = Integer.parseInt(numberString);

        /*
           Read and convert the second integer (see note just above).
        */
        numberString = JOptionPane.showInputDialog(
            "Enter second integer value:");
        number2 = Integer.parseInt(numberString);

        // Calculates and displays the answer
        JOptionPane.showMessageDialog(null, "Hello, " + name + "!" + "\n" +
            "The sum of " + number1 + " and " + number2 +
            " is " + (number1 + number2) + ".");
    }
}

And the error message is

C:\Documents and Settings\Admin\My Documents\Java Programs\SwingIO.java:34: cannot resolve symbol
symbol  : variable JOptionPane 
location: class SwingIO
        name = JOptionPane.showInputDialog("What is your first name?");
                       ^
C:\Documents and Settings\Admin\My Documents\Java Programs\SwingIO.java:41: cannot resolve symbol
symbol  : variable JOptionPane 
location: class SwingIO
        numberString = JOptionPane.showInputDialog(
                               ^
C:\Documents and Settings\Admin\My Documents\Java Programs\SwingIO.java:48: cannot resolve symbol
symbol  : variable JOptionPane 
location: class SwingIO
        numberString = JOptionPane.showInputDialog(
                               ^
C:\Documents and Settings\Admin\My Documents\Java Programs\SwingIO.java:53: cannot resolve symbol
symbol  : variable JOptionPane 
location: class SwingIO
        JOptionPane.showMessageDialog(null, "Hello, " + name + "!" + "\n" +
                ^
4 errors

Tool completed with exit code 1

Dani AI

Generated

— your source is fine and, as noted, the compiler doesn't know what JOptionPane refers to because it's part of Swing. The fact it builds on the school machine but not the laptop points to an environment/configuration issue rather than a logic error in your code.

Quick checklist and troubleshooting (in order of ease):

  • Verify which Java tools the laptop is actually using. Open a command prompt in the folder with SwingIO.java and run the commands below to see the versions and to try a manual compile. This separates TextPad problems from JDK problems.
javac -version
java -version
javac SwingIO.java
java SwingIO
  • If javac is missing or a different version than expected, point TextPad at the JDK bin directory (or adjust PATH/JAVA_HOME). TextPad often calls whatever javac is on PATH; if only a JRE is installed or PATH points to a different JDK, compile errors occur even though the source is correct.

  • Check for local name clashes or package problems: make sure there is no file named JOptionPane.java or a directory named javax in the same folder, and that there is no incorrect package declaration at the top of your source that doesn’t match the folder layout. Also verify the class name and file name match exactly and the .java extension is correct (case-sensitive on some setups).

A final note: Swing has been part of the standard JDK since very early releases, so no extra jars are needed. If the command-line compile works but TextPad still fails, update TextPad’s Java compile command to use the JDK javac and post the exact output of the failing compile command if you need more help.

Recommended Answers

All 2 Replies

Also i should have mentioned that i am useing the same compiler on my laptop that i us at school "Textpad". My school has Java 2 SDK ,SE v1.4.2_12 installed. I have that and J2SE Runtime Enviroment 5.0 update 6.

Any advise?

There is nothing wrong with your program nor your compiler. It is only that you forgot to include this statement:

import javax.swing.JOptionPane;

That is why the compiler doesn't recognise JOptionPane function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.