0

I need to write different methods for printing phrases with loops and I'm having trouble with printing an array. My assignment is to have each method print the phrase 5 times but I can't figure out how to do it with the array. This is what I have so far and it only prints once.

  String[] words = {"I'm ","Ready ","Now!"};
  for(int i=0; i < 5; i++)
  {
  System.out.print(words[i]);
  }
Tracy Nguyen
  • 55
  • 1
  • 6
  • One loop to count from 1 to 5, another inner one to count from 0 to the last element of the array. – Federico klez Culloca Nov 08 '18 at 16:56
  • You could use nested loops, the outer for loop to tell it to run 5 times, and the inner one which is the loop you already created inside. That would do it 5 times. But you would want to change your current loops `I < 5` to `I < arraylength - 1`. – arahman Nov 08 '18 at 16:56
  • 4
    I'm pretty sure this will throw an `ArrayOutOfBoundsException` – Nicholas K Nov 08 '18 at 16:56
  • 1
    Another approach is to create a String by [joining the array elements](https://stackoverflow.com/questions/1515437/java-function-for-arrays-like-phps-join), then use a for loop to print the String 5 times. – Andrew S Nov 08 '18 at 17:01

4 Answers4

0

below code will print the phrase 5 times.

String[] words = {"I'm ","Ready ","Now!"};
  for(int i=0; i < 5; i++)
  {
   for (String word: words) 
    {
     System.out.print(word);
    }
    System.out.println("");
  }
A_P
  • 21
  • 5
0

I wouldn't use nested loops. I'd first concatenate the words to form the phrase, then I would write the phrase five times, like this:

String[] words = { "I'm ", "Ready ", "Now!" };
String phrase = String.join("", words);

for (int i = 0; i < 5; i++) {
    System.out.println(phrase);
}

Prints:

I'm Ready Now!
I'm Ready Now!
I'm Ready Now!
I'm Ready Now!
I'm Ready Now!

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
0

I'm not sure whether the formatting is important but if not, you could use the Arrays.toString() method:

String[] words = {"I'm ","Ready ","Now!"};
for(int i=0; i < 5; i++)
{
  System.out.println(Arrays.toString(words));
}
ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
Robert Kock
  • 5,795
  • 1
  • 12
  • 20
-1

You can create a method. For instance it can be implemented like that.

public void printWords(String[] wordsArray, int repeatPhraseNumber) {
    for(int i=0;i<wordsArray.length;i++) {
       for(int repeatCount=0;repeatCount<repeatPhraseNumber;repeatCount++) {
           System.out.println(wordsArray[i]);
       }
    }
}

And you can call this like that, wordConsolePrinter is an instance of a class that has the printWords method. You can call it as follows.

String[] words = {"Hello","World"};
wordConsolePrinter.printWords(words,5);

And this will produce the following output.

Hello
Hello
Hello
Hello
Hello
World
World
World
World
World

If you need to print all words as a single sentence/phrase, the method should be modified as follows.

public void printWords(String[] wordsArray, int repeatPhraseNumber) {
    for(int repeatCount=0;repeatCount<repeatPhraseNumber;repeatCount++) {
       for(int i=0;i<wordsArray.length;i++) {
           System.out.print(wordsArray[i]);
           System.out.print(" "); // Space between words
       }
       System.out.println(""); // New line
    }
}

And this will output

Hello World
Hello World
Hello World
Hello World
Hello World

Of course this could be implemented without using methods.

This is a really easy task, please try to read more about loops and arrays.

CROSP
  • 4,499
  • 4
  • 38
  • 89