Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JOptionPane and StringBuilder

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question JOptionPane and StringBuilder

    I'm using the JOptionPane to display and receive information to/from the user. In addition to this, I am using StringBuilder to receive inputs from the user and then manipulate (append) that input to display the desired output.
    //Example

    (JOptionPane): "Enter text"

    (user input): Java is cool!

    (program): *stringBuilder.append(" I love it!")

    JOptionPane.showMessageDialog( );

    .....

    Hopefully you get the idea from this psuedo-like code. The problem is that showMessageDialog(null, string) and not showMessageDialog(string, string). I want to display the enter text: " Java is cool! I love it! " in the JOptionPane. How do I accomplish this, since showMessageDialog only accepts (null, string).


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JOptionPane and StringBuilder

    problem is that showMessageDialog(null, string) and not showMessageDialog(string, string)
    Why can't you use the version that works with a single String? What is the reason you think you need two Strings?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane and StringBuilder

    If I want to show the first text "Java is cool!" using JOptionPane, then append.("I love it!) to the text, "Java is cool!" does not appear. So I assumed I needed to use (string, stringBuilder.toString() ) instead of (null, stringBuilder.toString() )

    public class builderMain {
     
    	public static void main(String[] args) {
    		String hello = JOptionPane.showInputDialog( null, "Enter Text" , "String Builder", JOptionPane.INFORMATION_MESSAGE);
    		JOptionPane.showMessageDialog(null, hello, "String Builder", JOptionPane.INFORMATION_MESSAGE);
     
    		StringBuilder stringBuilder = new StringBuilder();
     
    		//stringBuilder.append("");
    		stringBuilder.append( " I love it!" );
    		stringBuilder.capacity();
     
    		JOptionPane.showMessageDialog( null, stringBuilder.toString() , "String Builder", JOptionPane.INFORMATION_MESSAGE);
     
    		String yes =JOptionPane.showInputDialog(null, "Enter the string ' Yes, '");
    		stringBuilder.insert(16, yes);
    		JOptionPane.showMessageDialog( null, stringBuilder.toString() , "String Builder", JOptionPane.INFORMATION_MESSAGE);

    Why does " Java is cool! " not remain in the stringBuilder when I compile? It says, "I love Yes, it!"
    Last edited by javaStooge; March 6th, 2014 at 10:29 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JOptionPane and StringBuilder

    I don't see where the code is appending the String: "I love it!" to the String: "Java is cool!"?
    Either use the + operator to concatenate the two Strings
    or put them both into StringBuilder and use its toString() method to get the String with them both in it.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    javaStooge (March 6th, 2014)

  6. #5
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane and StringBuilder

    I see where my error is... StringBuilder stringBuilder = new StringBuilder(hello);

    I didn't assign an initial value (hello) in the constructor...

Similar Threads

  1. what is string and StringBuffer and StringBuilder?
    By shirin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2014, 08:03 AM
  2. clearing StringBuilder
    By tonynsx in forum Java Theory & Questions
    Replies: 6
    Last Post: October 31st, 2013, 10:28 PM
  3. StringBuilder Vs StringBuffer
    By qazi in forum Java Theory & Questions
    Replies: 0
    Last Post: July 28th, 2013, 08:31 AM
  4. StringBuilder
    By Anoop Shiralige in forum Java Theory & Questions
    Replies: 1
    Last Post: June 23rd, 2012, 07:08 AM
  5. [SOLVED] New line and StringBuilder
    By newbie in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 1st, 2011, 09:42 PM