I think i have everything write except for the Input statements. It is telling me that JOptionPane can't be resolved.
Here is what I have so far.

public class Main_Class {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String firstNumber;
        String secondNumber;
        int input1;
        int input2;
        int sum; 
        int difference ;
        int product;
        double average;
            //read in first number as a string
        firstNumber = JOptionPane.showInputDialog("Enter first integer");

            //read in second number as a string
        secondNumber = JOptionPane.showInputDialog("Enter second integer");

            //convert numbers from String to type in
        input1 = Integer.parseInt(firstNumber);
        input2 = Integer.parseInt(secondNumber);

            //add the numbers
        sum = input1 + input2;

            //display the results
        JOptionPane.showMessageDialog(null,"The sum is") +sum'
                                      "Result", JOptionPane.PLAIN_MESSAGE);


    }   


}

Recommended Answers

All 10 Replies

That's probably because you didn't import anything. In order to use the class you have to import it. I would guess the import is "import javax.swing.*;". You put imports above your class body.

As mentioned above you have to import the swing libraries that contain JOptionPane. Also I've noticed you have a syntax error in your final message dialog call, probably messed it up as you were splitting the line to be two. The correct one should be:

JOptionPane.showMessageDialog(null, "The sum is " + sum + ".", "Result", JOptionPane.PLAIN_MESSAGE);

If you break that one up, just put a break in front of the first + or something.

Your Error in JOptionPane code write That code as Follows

JOptionPane.showMessageDialog((Component) null, "Sum is:-", " your sum is " + String.valueOf(sum),JOptionPane.PLAIN_MESSAGE);

Your Error in JOptionPane code write That code as Follows

JOptionPane.showMessageDialog((Component) null, "Sum is:-", " your sum is " + String.valueOf(sum),JOptionPane.PLAIN_MESSAGE);

Is this not exactly the same as what has been adviced in the previous post ? Whats the point of this then ?

1). Cast null with Component
2). Convert sum variable to string type
3).Change Syntax as follows

JOptionPane.showMessageDialog(<Component parent>,<String Title>,<String message>,<Message type>);

My friend your first mistake is that you forgot about converting sum variable int to String that cause an error OK

1). Cast null with Component
2). Convert sum variable to string type

Both of these aren't required at all.
1. NULL does not need to be casted to anything.
2. If the int is within a string, it's acceptable.

commented: Glad that you corrected that nonsense. +18

This is what I have now and i still can't figure out what I am doing wrong. I can't figure out where to put "import javax.swing.JOptionPane;"

public class Main_Class {

    /**
     * @param args
     */

    public static void main(String[] args) {
        import javax.swing.JOptionPane;
        // TODO Auto-generated method stub
        String firstNumber;
        String secondNumber;
        int input1;
        int input2;
        int sum; 
        int difference ;
        int product;
        double average;

            //read in first number as a string
        firstNumber = JOptionPane.showInputDialog("Enter first integer");

            //read in second number as a string
        secondNumber = JOptionPane.showInputDialog("Enter second integer");

            //convert numbers from String to type in
        input1 = Integer.parseInt(firstNumber);
        input2 = Integer.parseInt(secondNumber);

            //add the numbers
        sum = input1 + input2;

            //display the results
        JOptionPane.showMessageDialog(null, "The sum is " + sum + ".", "Result", JOptionPane.PLAIN_MESSAGE);
            //subtract the numbers
        difference = input1 - input2;

            //display the results
        JOptionPane.showMessageDialog(null, "The difference is " + difference + ".", "Result", JOptionPane.PLAIN_MESSAGE);
            //multiply the numbers
        product = input1 * input2;

            //display the results
        JOptionPane.showMessageDialog(null, "The Product is " + product + ".", "Result", JOptionPane.PLAIN_MESSAGE);
            //average of the numbers
        average = input1 + input2 / 2;

            //display the results
        JOptionPane.showMessageDialog(null, "The average is " + average + ".", "Result", JOptionPane.PLAIN_MESSAGE);




    }

}

imports should be declared before the public class definition. They should be the immediate statements after the package statements. To give a skeleton forthis:

// package declaration should be the very first stmts, in your .java file, since this is a comment you can omit this.
package mypackage;

// next comes the import
import javax.swing.JOptionPane;
// other imports

// then follows the public class
public class Demo{
    
    // member methods and variables go here
    
    // in the public class sits the main
    public static void main(String [] args){
    // code blocks
    }
}

You tried to import in the main method. You must import all java libraries outside of the class declaration, for example:

import javax.swing.JOptionPane;

public class Foo
{
          public static void main(String[] args)
          {
                    // Your code, etc.
                    JOptionPane.showMessageDialog(null, "This is a message.", "Title", JOptionPane.PLAIN_MESSAGE);
          }
}

EDIT: Sorry, for double post, verrukt posted while I was typing mine :)

I think I figured it out thank you all for the help.

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.