Jokes February 2016

Continuing Part Of String On New Line

String noun1 = jTextField1.getText();
        String noun2 = jTextField1.getText();
        String noun3 = jTextField1.getText();

        String verb1 = jTextField1.getText();
        String verb2 = jTextField1.getText();
        String verb3 = jTextField1.getText();

        String adj1 = jTextField1.getText();
        String adj2 = jTextField1.getText();
        String adj3 = jTextField1.getText();

        String story = "This is the story about a " + noun1;

        jTextArea1.setText(story);

I'm creating a Madlibs with JFrame and need to find out how to continue string on a new line (I forgot) Such as story. I need it to continue so I can add the other sentences.

Answers


ctst February 2016

You mean a linebreak in the String? Then the code you are looking for is "\n". E.g. the output of

String text = "Hello \nWorld";

Would be

Hello
World


Nikolas Charalambidis February 2016

Use a line separator

String story = "This is the story about a" + System.lineSeparator() + noun1;

Or use \n for a new line

String story = "This is the story about a\n" + noun1;

Also you can use more System.out.println(...) commands when you want to print it out.

System.out.println("This is the story about a"); // Print the string inside and add a linebreak afterwards
System.out.println(noun1); // Print the value of noun1 variable

All of them will give you the following result:

This is the story about a
"your story name"

More information is here: Java String new line

Post Status

Asked in February 2016
Viewed 2,024 times
Voted 12
Answered 2 times

Search




Leave an answer