Anyways, one of the exercises was to get two numbers from the user and display the sum using dialog boxes. This is what I have so far.
package additiondialog;
import javax.swing.JOptionPane;
public class AdditionDialog
{
public static void main(String[] args)
{
//prompt user to enter first number
String firstNumber = JOptionPane.showInputDialog("Enter your first number");
//prompt user for second number
String secondNumber = JOptionPane.showInputDialog("Enter your second number");
//convert strings to int values for use in calculation
int number1 = Integer.parseInt(firstNumber);
int number2 = Integer.parseInt(secondNumber);
int sum = number1 + number2; //add numbers
//display the sum of the two number user inputted
JOptionPane.showMessageDialog(null, "The sum is " +sum);
}//end main
}//end class AdditionDialog
I was just curious how I would get the two numbers to display along with the sum in the dialog box. The sum of 5+5 is 10, etc.
---------- Post added at 12:34 AM ---------- Previous post was at 12:31 AM ----------
I freaking got it!!!!!! That was easy. Here's what I did just in case anyone else wants to know.
JOptionPane.showMessageDialog(null, "The sum of "+ number1 + "+" + number2 + "=" +sum);
Just got done reading Chapter 3 and was introduced to JOptionPane dialogs at the very end. I was getting confused because I thought I had to use %s or whatever to display the input.
Thanks!


Sign In
Create Account


Back to top









