"This is a line"
needs to be
"line a is This"
We must do this without using .split or any other classes in java so I am just using substring.
Here is the code for my method and beneath that is what it is outputting. I am struggling to figure out why it is reading the words like it is.
The array of words holds the following .txt file with each element being a line of text. int lines is the number of lines which is 14.
"Two households, both alike in dignity,
In fair Verona, where we lay our scene,
From ancient grudge break to new mutiny,
Where civil blood makes civil hands unclean.
From forth the fatal loins of these two foes
A pair of star-cross'd lovers take their life;
Whole misadventured piteous overthrows
Do with their death bury their parents' strife.
The fearful passage of their death-mark'd love,
And the continuance of their parents' rage,
Which, but their children's end, nought could remove,
Is now the two hours' traffic of our stage;
The which if you with patient ears attend,
What here shall miss, our toil shall strive to mend."
public void reverseText(String[]words, int lines, String line)
{
int lastSpace = 0;
int i;
char ch;
String word;
String str = " ";
for(int j = 0; j < lines; j++)
{
str = words[j];
lastSpace = words[j].length();
for(i=lines-1; i >= 0; i--)
{
ch = str.charAt(i);
if(ch == ' ')
{
word = str.substring(i + 1, lastSpace);
System.out.print(word + " ");
lastSpace = i;
}
}
word = str.substring(0, lastSpace);
System.out.println(word);
}
}
Here is the output:
"households, both alike in dignity, Two
Verona, where we lay our scene, fair In
grudge break to new mutiny, ancient From
blood makes civil hands unclean. civil Where
the fatal loins of these two foes forth From
star-cross'd lovers take their life; of pair A
misadventured piteous overthrows Whole
death bury their parents' strife. their with Do
passage of their death-mark'd love, fearful The
continuance of their parents' rage, the And
their children's end, nought could remove, but Which,
two hours' traffic of our stage; the now Is
you with patient ears attend, if which The
shall miss, our toil shall strive to mend. here What"

New Topic/Question
Reply



MultiQuote


|