1

I have already written simple random phrase generator. But I don't understand how to get rewrite this program using stringbuilder. I tried to use "append". but it just adds words into overall string.

My code:

public static void main(String[] args){
    String[] firstWord = {"one", "two","three"};
    String[] secondWord = {"four", "five", "six"};
    String[] thirdWord = {"seven", "eight", "nine"};
    String[] fourthWord = {"ten", "eleven", "twelve"};

    int oneLength = firstWord.length;
    int secondLength = secondWord.length;
    int thirdLength = thirdWord.length;
    int fourthLength = fourthWord.length;

    int rand1 = (int) (Math.random() * oneLength);
    int rand2 = (int) (Math.random() * secondLength);
    int rand3 = (int) (Math.random() * thirdLength);
    int rand4 = (int) (Math.random() * fourthLength);

    String phrase = firstWord[rand1] + " " + secondWord[rand2] + " " 
                    + thirdWord[rand3] + fourthWord[rand4];
    System.out.println(phrase);
}
6
  • 2
    FYI, fourthWord is not present in the code shared above. Commented Mar 4, 2017 at 18:06
  • 2
    Could you provide an example with StringBuilder which did not work for you, please. Commented Mar 4, 2017 at 18:07
  • Ok, I've corrected this Commented Mar 4, 2017 at 18:08
  • gist.github.com/anonymous/b3d36364469f38253122742495cdff30 Commented Mar 4, 2017 at 18:10
  • 1
    you coded it right on random, what you dont understand Commented Mar 4, 2017 at 18:15

2 Answers 2

2

Like this:

String phrase = new StringBuilder(firstWord[rand1]).append(" ")
                    .append(secondWord[rand2]).append(" ")
                    .append(thirdWord[rand3]).append(" ")
                    .append(fourthWord[rand4]).toString();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But why do You use .toString method?
That's required to convert the StringBuilder into a String, they're different types. Try removing the toString() - it won't compile.
1

Your example modified to use string builder. You can test it at https://www.tutorialspoint.com/compile_java8_online.php

    import java.lang.StringBuilder;

public class HelloWorld{

     public static void main(String []args){

    String[] firstWord = {"one", "two","three"};
    String[] secondWord = {"four", "five", "six"};
    String[] thirdWord = {"seven", "eight", "nine"};

    int oneLength = firstWord.length;
    int secondLength = secondWord.length;
    int thirdLength = thirdWord.length;


    int rand1 = (int) (Math.random() * oneLength);
    int rand2 = (int) (Math.random() * secondLength);
    int rand3 = (int) (Math.random() * thirdLength);


    String phrase = firstWord[rand1] + " " + secondWord[rand2] + " " 
                    + thirdWord[rand3];

    StringBuilder sb = new StringBuilder();
    sb.append(firstWord[rand1]);
    sb.append(" ");
    sb.append(secondWord[rand2]);
    sb.append(" ");
    sb.append(thirdWord[rand3]);

    String phraseSb = sb.toString();

        System.out.println("Plus Operator: " + phrase);
        System.out.println("String Builder: " + phraseSb);

     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.