Results 1 to 8 of 8
  1. #1
    fthnm2005 is offline Member
    Join Date
    Jul 2008
    Location
    Tashkent, Uzbekistan
    Posts
    6
    Rep Power
    0

    Default [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 class
    Last edited by fthnm2005; 07-14-2008 at 03:48 AM.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Post setMessage

    JOptionPane (Java 2 Platform SE v1.4.2)
    Java Code:
    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.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    fthnm2005 is offline Member
    Join Date
    Jul 2008
    Location
    Tashkent, Uzbekistan
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Nicholas Jordan View Post

    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!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default

    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.

  5. #5
    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

    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. :)

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Talking as Eranga clarifies Norm

    Quote Originally Posted by fthnm2005 View Post
    Thanks for the post. But I did not understand...
    Eranga's clarification of Norm's efforts is correct.
    Quote Originally Posted by fthnm2005 View Post
    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.

    Java 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());
    }
    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.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  7. #7
    fthnm2005 is offline Member
    Join Date
    Jul 2008
    Location
    Tashkent, Uzbekistan
    Posts
    6
    Rep Power
    0

    Default

    Thank you all very much! Helped a lot!

  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

Similar Threads

  1. my first jsp code
    By munna_dude in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-29-2008, 09:14 AM
  2. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  3. code help
    By chitwood in forum New To Java
    Replies: 0
    Last Post: 02-10-2008, 12:08 AM
  4. tic tac toe code
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 04:36 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •