I wonder how can I change my code so, that the number of given value words was specified in another class. And in construction class I need to make a method that prints in every given value. Here is my code:
public void wordBox()
{
for (int i = 0; i < words.length(); i++)
{
if (words.length() % wordsPerLine == 0 && words.charAt(wordsPerLine) == ' ')
System.out.println();
else
System.out.print(words.charAt(i));
}
System.out.println();
}
wordsPerLine was given in another class as a number of 3.
String is this:”the quick brown fox jumped over the lazy dog”
output should be look like:
the quick brown
fox jumped over
the lazy dog
Note: I am not allowed to use any other classes like token or buffer. Also, I cannot use replace/split. So, this is what I was given and I need to work from that.
Any help will be appreciated.
Thanks
Rather than making
wordsandwordsPerLineinstance variables that are set in the constructor, make them parameters passed to your static method, like this:This way you would be able to specify
wordsandwordsPerLineindependently of the constructor, and callwordBoxfrom anywhere with any combination of words and the desired number of words per line.Here is a link to ideone with this running program.