Jump to content

How to display input into dialog in Java?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
wolfman

wolfman

    Newbie

  • Members
  • PipPip
  • 18 posts
I know this is probably an easy answer, but I can't figure out how to do it. I'm trying to learn as much Java before I start my class in the next 3 months. Purchased a book and using the internet to teach myself. Posted here before using Pseudo code, but Java is a different dog and a real language.

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!
-=[ w | o | l | f | m | a | n ]=-

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
JOptionPane.showMessageDialog(null, "The sum of " + number1 + "+" + number2 + "is " + sum);





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users