Results 1 to 5 of 5
  1. #1
    Nifras is offline Member
    Join Date
    Nov 2009
    Location
    Puttalam, Sri Lanka.
    Posts
    6
    Rep Power
    0

    Cool Is this right!! Java Swing

    import javax.swing.JOptionPane;

    public class Addition_GUI
    {
    public static void main(String args[])
    {
    String firstInt;
    String secondInt;

    firstInt = JOptionPane.showInputDialog("Enter the First Integer value :");
    Integer num1 = Integer.parseInt(firstInt);

    secondInt = JOptionPane.showInputDialog("Enter the Second Integer value :");
    Integer num2 = Integer.parseInt(secondInt);

    Integer sum = num1 + num2;

    String result = String.format("The sum is : %s",sum);

    JOptionPane.showMessageDialog(null,result);
    }
    }

    It didn't give any errors. But, I need a explanation. Please help me.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    But, I need a explanation.
    About what?

    Also, use the "Code tags" when posting code to the formatting is retained and the code is more readable.

  3. #3
    Nifras is offline Member
    Join Date
    Nov 2009
    Location
    Puttalam, Sri Lanka.
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    About what?

    Integer sum = num1 + num2;

    String result = String.format("The sum is : %s",sum);.
    Sum is declared as Integer and In second line, It pass its value to a String format specifier.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Nifras View Post
    Sum is declared as Integer and In second line, It pass its value to a String format specifier.
    So Java automatically invokes toString().

    A couple of other points:

    1. You don't really need two String variables, since you're done with the first by the time you use the second. It might make your code clearer if you had just one String variable called input or something along those lines.

    2. Your Integer variables can all be primitive int variables instead. Generally it's good practice to use primitives where you can, although in this case it certainly wouldn't make much difference.

    -Gary-

  5. #5
    Nifras is offline Member
    Join Date
    Nov 2009
    Location
    Puttalam, Sri Lanka.
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Help with java.swing.Jlist
    By mitsukai in forum AWT / Swing
    Replies: 2
    Last Post: 04-23-2009, 01:12 PM
  2. Java swing question
    By Godsent in forum New To Java
    Replies: 4
    Last Post: 04-18-2009, 01:00 PM
  3. [swing] Java and sql
    By ezee in forum AWT / Swing
    Replies: 1
    Last Post: 03-27-2009, 05:55 PM
  4. Java Swing
    By RAMYA in forum New To Java
    Replies: 3
    Last Post: 11-12-2008, 07:00 AM
  5. Java Swing
    By JavaBean in forum Java Tutorial
    Replies: 0
    Last Post: 09-22-2007, 08:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •