|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-14-2008, 03:57 AM
|
|
Member
|
|
Join Date: Jul 2008
Location: Tashkent, Uzbekistan
Posts: 6
|
|
|
[SOLVED] Help in the Code, please!
Hello all: I need a help in this simple program, whcih is almost complete, but one more requirement:
This is a simple program to select and display the Maximum number among 3 numbers eneterd by the user, and display it along with the Sum and Product of them in ONE MESSAGE DIALOG, but in different lines. I was able to display with seperate mes.dial. windows, but can not figure it out how to incorporate it into JOptionPane method. Can someone help, please?
Here is the complete program:
import javax.swing.JOptionPane;
public class MaxSumProduct {
public static void main(String[] args) {
//variable declaration
String firstNumber, secondNumber, thirdNumber;
int number1, number2, number3, max, sum, product;
//
firstNumber=JOptionPane.showInputDialog("Please, enter the first number: ");
secondNumber=JOptionPane.showInputDialog("Please, enter the second number: ");
thirdNumber=JOptionPane.showInputDialog("Please, enter the second number: ");
number1=Integer.parseInt(firstNumber);
number2=Integer.parseInt(secondNumber);
number3=Integer.parseInt(thirdNumber);
max=number1;
sum=(number1+number2+number3);
product=(number1*number2*number3);
if(max<number2) max=number2;
if(max<number3) max=number3;
JOptionPane.showMessageDialog(null,"The Maximum is: "+max);
JOptionPane.showMessageDialog(null,"The Sum is: "+sum);
JOptionPane.showMessageDialog(null,"The Product is: "+product);
System.exit(0);
}//End method
}//end class
Last edited by fthnm2005 : 07-14-2008 at 05:48 AM.
|
|

07-14-2008, 04:45 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 780
|
|
|
setMessage
JOptionPane (Java 2 Platform SE v1.4.2)
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);
Handy for getting data, I eventually went to using a file.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. .
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
|
|

07-14-2008, 05:47 AM
|
|
Member
|
|
Join Date: Jul 2008
Location: Tashkent, Uzbekistan
Posts: 6
|
|
Originally Posted by Nicholas Jordan
Handy for getting data, I eventually went to using a file.
Thanks for the post. But I did not understand... I mean how do I incorporate your code into the whole program? Or did we misunderstand each other?
thanks!
|
|

07-14-2008, 06:43 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
Could you explain a little more what your program is not doing correctly?
To have a multiline output, use the newline character in your message text: "line1\nline2";
Also read the API doc for all the different ways to use JOptionPane. And then write a program to use as many of them as you can to see how they each work.
|
|

07-14-2008, 07:09 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
|
|
I think he is looking what Norm says, multiple lines on JOptionPane. Simply use new line characters as Norma explain. This is quite simple thing that you should try before asking more pal. If you know how to use multiple lines generally, why don't you try it here too. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-14-2008, 07:14 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 780
|
|
|
as Eranga clarifies Norm
Originally Posted by fthnm2005
Thanks for the post. But I did not understand...
Eranga's clarification of Norm's efforts is correct.
Originally Posted by fthnm2005
I mean how do I incorporate your code into the whole program?
What I did was grab some sample code from the documentation. What I think, mostly from memory but I am reasonalby sure, is do a new JDialogBox has to be setMessage() in constructor. Actually, what I think I did wrong was not to look at you code a little closer.
I was thinking the Dialog would have a set message method call, maybe it does not. The way you are doing it, to get the three numbers all in the same ( displayed window ) can be done two ways. One is to build a string of the three numbers. The other, essentially the same, is to put all three numbers in one string.
package demo;
// Constructs a string builder initialized to the contents of the specified string.
class HelpTheCode
{
String str = new String("The maximum is: ");
static final String lineSeparator = System.getProperty("line.terminator");//
StringBuilder showEm = StringBuilder(str) ;
showEm.append(number1.toString());
showEm.append(lineSeparator);//
showEm.append(("The number is: ");
showEm.append(number2.toString());
showEm.append(lineSeparator);//
showEm.append(("The product is: ");
showEm.append(number3.toString());
}
See line separator : Java Glossary
If you are using this in a organized study enviornment the instructor will spot the use of static final line ending and know that you have been getting help. Instead of doing that, you can use "\n" to separate the strings, but it does not work as well. See the cited link.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. .
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
|
|

07-15-2008, 01:47 AM
|
|
Member
|
|
Join Date: Jul 2008
Location: Tashkent, Uzbekistan
Posts: 6
|
|
|
Thank you all very much! Helped a lot!
|
|

07-15-2008, 05:49 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
|
|
|
Nice, if you solve the question place mark the thread resolved. It's really helpful to all of us.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|