Skip to content
Advertisement

Separate “for” loop iterations

How would I go about making each iteration of my four loop be random. The outcome as of now makes all 20 sentences put the same output, although it is choosing random words each time from the four arrays created in static class OuterWord. I have omitted the parts of the code that I don’t think are necessary including the getMethods created for each array and sentence, hopefully that will not negatively effect my question.

`public class SentenceMaker {

static class OuterWord {
    //arrays
    String [] article = {"the", "a", "one", "some", "any"}; //array for articles
    String [] noun = {"boy", "girl", "dog", "town", "car"}; //array for nouns
    String [] verb = {"drove", "jumped", "ran", "walked", "skipped"}; //array for verbs
    String [] preposition = {"to", "from", "over", "under", "on"}; //array for prepositions

}
static class OuterSentence {
   int random = (int) (Math.random()*5);
    OuterWord word = new OuterWord();

    //sentence structure article-noun-verb-preposition-article-noun
    //StringBuilder with append, attempts to concatenate with .append
    //This code works as well
    StringBuilder sentence = new StringBuilder()
                    .append(word.article[random].substring(0, 1).toUpperCase())
                    .append(word.article[random].substring(1))
                    .append(" ")
                    .append(word.noun[random])
                    .append(" ")
                    .append(word.verb[random])
                    .append(" ")
                    .append(word.preposition[random])
                    .append(" ")
                    .append(word.article[random])
                    .append(" ")
                    .append(word.noun[random])
                    .append(".");
}


public static void main(String args[]) {
    //random element from arrays
    //int random = (int) (Math.random()*5);

    //instantiation of the Word and Sentence classes
    OuterWord word = new OuterWord();
    OuterSentence s = new OuterSentence();


    //loop initializer
    int counter = 0;

    //for loop for 20 iterations
    for(int i = 0; i < 20; i++){
        //counter to keep track of iterations
        counter = counter + 1;
        //sentence article-noun-verb-preposition-article-noun
        System.out.println("Sentence " + counter + ": " + s.sentence.);
    }
}

}`

Advertisement

Answer

Just move the OuterSentence object to for loop.

for(int i = 0; i < 20; i++){
        OuterSentence s = new OuterSentence();
        //counter to keep track of iterations
        counter = counter + 1;
        //sentence article-noun-verb-preposition-article-noun
        System.out.println("Sentence " + counter + ": " + s.sentence);
    }

The above one is much readable or the other way is to just create the new object in the System.out.println statement like as follows:

System.out.println("Sentence " + counter + ": " + new OuterSentence().sentence);

In both the ways we are creating a new object for every statement so that new random will get generated so that new sentence will be formed.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement