Results 1 to 8 of 8
- 07-14-2008, 01:57 AM #1
Member
- Join Date
- Jul 2008
- Location
- Tashkent, Uzbekistan
- Posts
- 6
- Rep Power
- 0
[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:
Java Code: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 classLast edited by fthnm2005; 07-14-2008 at 03:48 AM.
- 07-14-2008, 02:45 AM #2
setMessage
JOptionPane (Java 2 Platform SE v1.4.2)
Handy for getting data, I eventually went to using a file.Java Code:Object[] possibleValues = { "First", "Second", "Third" }; Object selectedValue = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 07-14-2008, 03:47 AM #3
Member
- Join Date
- Jul 2008
- Location
- Tashkent, Uzbekistan
- Posts
- 6
- Rep Power
- 0
- 07-14-2008, 04:43 AM #4
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, 05:09 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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. :)
- 07-14-2008, 05:14 PM #6
as Eranga clarifies Norm
Eranga's clarification of Norm's efforts is correct.
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.
See line separator : Java GlossaryJava Code: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()); }
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.Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 07-14-2008, 11:47 PM #7
Member
- Join Date
- Jul 2008
- Location
- Tashkent, Uzbekistan
- Posts
- 6
- Rep Power
- 0
Thank you all very much! Helped a lot!
- 07-15-2008, 03:49 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Nice, if you solve the question place mark the thread resolved. It's really helpful to all of us.
Similar Threads
-
my first jsp code
By munna_dude in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 04-29-2008, 09:14 AM -
I need help fixing my code.. or non code?
By MrHuggykins in forum New To JavaReplies: 1Last Post: 03-19-2008, 10:12 PM -
code help
By chitwood in forum New To JavaReplies: 0Last Post: 02-10-2008, 12:08 AM -
tic tac toe code
By zoe in forum New To JavaReplies: 1Last Post: 07-23-2007, 04:36 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks