Results 1 to 13 of 13
  1. #1
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Exclamation Help with JOptionPane

    So this is the first time ive ever used JPanes so i have some questions

    first is how do i do math with them?
    it just concatenates the 2 numbers

    Second is i saw an example and they used a buffer reader
    Why?

    And third can someone help me with this code it just concatenates the two numbers and ive tryed everything

    All help is appreciated:)

    Java Code:
    import java.io.*;
    import javax.swing.*;
    import java.util.Random;
    
    
    public class CasinoGame{
    
    
      
    public static void main(String[] args)throws IOException{
    	
    	
        String say;
    	String say2;
    	String say3;
        say= JOptionPane.showInputDialog(null, "Type in 1 number");
    	say2= JOptionPane.showInputDialog(null, "Type in 1 more number");
    	say3=say+say2;
    	JOptionPane.showMessageDialog(null, "The sum of the two numbers is "+say3);
    	
    	
    }
    }
    Are you suggesting that Cocunuts migrate?!! -Monty Python

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

    Default

    So this is the first time ive ever used JPanes so i have some questions
    I have a question to, whats a "JPane"?

    first is how do i do math with them?
    Use Integer.parseInt(...) to get an int value you can use in your math formulas

    Second is i saw an example and they used a buffer reader, Why?
    I have no idea why?

    And third can someone help me with this code it just concatenates the two numbers
    How is this different than the first question?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    A showInputDialog returns a String, and this is important because if you do this:
    Java Code:
    "2" + "2"
    you'll get "22", not what you want.

    To add, you must use numbers, and to get numbers from a String, you must parse. If I were you, I'd look into use the Integer.parseInt(...) method to parse each String into a number and then add the number.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    String concatenation have different meanings in different cases. For an example following line of code,

    Java Code:
    System.out.println(1 + 1);
    It'll give 2 as the output. But in your case compiler seen them as Strings, simply concatenate.

    Java(tm) String Operations

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

    Default

    String concatenation have different meanings in different cases. For an example following line of code,
    String concatenation is always string concatenation.

    System.out.println("1" + "1") displays "11" because you are concatenating 2 strings.

    System.out.println(1 + 1) displays 2 because you are adding two integers.

    System.out.println(1 + "1") displays "11" because the integer 1, is converted to a string and string concatenation is done.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by camickr View Post
    String concatenation is always string concatenation.

    System.out.println("1" + "1") displays "11" because you are concatenating 2 strings.

    System.out.println(1 + 1) displays 2 because you are adding two integers.

    System.out.println(1 + "1") displays "11" because the integer 1, is converted to a string and string concatenation is done.
    I think you've misunderstand my comment. I'm talking about the logical meaning of that. As your examples in first line of code, deals with strings, you define them as it is. In the second line it's just and an addition of integer values, and the last on is just mixing them. Same thing I'm pointed, may be the explanation not clear to you.

  7. #7
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    So im still not understanding im using the parseInt an i dont think im using it right

    Heres my code

    All help is appreciated:)

    Java Code:
    public class CasinoGame{
    
    
      
    public static void main(String[] args)throws IOException{
    	
    	
        String say;
    	String say2;
    	String say3;
    	
        say= JOptionPane.showInputDialog(null, "Type in 1 number");
        Integer.parseInt(say);
    	say2= JOptionPane.showInputDialog(null, "Type in 1 more number");
    	Integer.parseInt(say2);
    	say3=say+say2;
    	JOptionPane.showMessageDialog(null, "The sum of the two numbers is "+say3);
    	
    	
    }
    }
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  9. #9
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    What?:confused:
    Are you suggesting that Cocunuts migrate?!! -Monty Python

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

    Default

    Did you read the API to see what value is returned from the parseInt() method?

    Did you search the forum or the web to find examples that use the method in question?

    Learn how to do some basic problem solving on your own. You learn more and its faster when you don't have to wait and hope someone spoonfeeds you the code.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Here is an simple explanation.

    Java Code:
        // Declaring two String variables (objects)
        String say, say2;
    
        // Declaring two integer variable
        int val1, val2;
    
        // Get the user input
        say= JOptionPane.showInputDialog(null, "Type in 1 number");
        say2= JOptionPane.showInputDialog(null, "Type in 1 more number");
    
        // Converting those strings into integer values
        val1 = Integer.parseInt(say);
        val2 = Integer.parseInt(say2);
    
        // Display the result
        JOptionPane.showMessageDialog(null, "The sum of the two numbers is " + (val1 + val2));

  12. #12
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    im pretty new whats API?
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. JOptionPane Troubles
    By Tb0h in forum New To Java
    Replies: 4
    Last Post: 08-26-2009, 01:12 PM
  2. help with JOptionPane.showMessageDialog()
    By gmn1 in forum New To Java
    Replies: 8
    Last Post: 03-30-2009, 09:31 AM
  3. JOptionpane
    By tommyyyy in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 08:33 AM
  4. JOptionPane
    By Mir in forum New To Java
    Replies: 5
    Last Post: 11-29-2008, 02:04 AM
  5. JOptionPane
    By whosadork in forum New To Java
    Replies: 2
    Last Post: 10-23-2008, 02:17 AM

Posting Permissions

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