Results 1 to 13 of 13
Thread: Help with JOptionPane
- 11-02-2009, 05:20 AM #1
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
- 11-02-2009, 05:51 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
I have a question to, whats a "JPane"?So this is the first time ive ever used JPanes so i have some questions
Use Integer.parseInt(...) to get an int value you can use in your math formulasfirst is how do i do math with them?
I have no idea why?Second is i saw an example and they used a buffer reader, Why?
How is this different than the first question?And third can someone help me with this code it just concatenates the two numbers
-
A showInputDialog returns a String, and this is important because if you do this:
you'll get "22", not what you want.Java Code:"2" + "2"
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.
- 11-02-2009, 06:24 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
String concatenation have different meanings in different cases. For an example following line of code,
It'll give 2 as the output. But in your case compiler seen them as Strings, simply concatenate.Java Code:System.out.println(1 + 1);
Java(tm) String Operations
- 11-02-2009, 06:37 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
String concatenation is always string concatenation.String concatenation have different meanings in different cases. For an example following line of code,
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.
- 11-02-2009, 01:34 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 11-03-2009, 03:52 AM #7
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
- 11-03-2009, 04:04 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
To which you refer this,
Java Code:Integer.parseInt(say2);
- 11-03-2009, 04:08 AM #9
What?:confused:
Are you suggesting that Cocunuts migrate?!! -Monty Python
- 11-03-2009, 04:08 AM #10
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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-03-2009, 04:09 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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));
- 11-03-2009, 04:11 AM #12
im pretty new whats API?
Are you suggesting that Cocunuts migrate?!! -Monty Python
- 11-03-2009, 04:12 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
JOptionPane Troubles
By Tb0h in forum New To JavaReplies: 4Last Post: 08-26-2009, 01:12 PM -
help with JOptionPane.showMessageDialog()
By gmn1 in forum New To JavaReplies: 8Last Post: 03-30-2009, 09:31 AM -
JOptionpane
By tommyyyy in forum New To JavaReplies: 2Last Post: 03-20-2009, 08:33 AM -
JOptionPane
By Mir in forum New To JavaReplies: 5Last Post: 11-29-2008, 02:04 AM -
JOptionPane
By whosadork in forum New To JavaReplies: 2Last Post: 10-23-2008, 02:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks