Название может показаться запутанным.
Просто у меня есть файл, в котором есть текст.Я использовал сканер, чтобы прочитать этот текст.Я сохранил этот текст в ArrayLists, который является строкой.Проблема у меня заключается в том, что мне нужно, чтобы метод обращал текст в этом ArrayList.Текст меняется, но не полностью.Мне нужно, чтобы слова тоже были в обратном порядке, но мой String ArrayList разбит на предложения.
Поэтому мне нужна помощь, чтобы добраться до каждого ArrayList и разбить эти слова на другие "", а затем поместить их в мой метод, чтобы он мог правильно перевернуть слова.
То, чего я хочу достичь, - это сначала разорвать строки и перевернуть слова.Сложная часть по-прежнему возвращает весь текст в строках.Мой профессор рекомендовал создать вспомогательные методы для решения этой проблемы.Пожалуйста помоги.
package thePackage;
/**
* Adan Vivero
* 12.6.18
* In this lab, I will be working with classes,
ArrayLists, and files
* mainly.
*/
import java.io.File;
import java.util.ArrayList;
import java.io.*;
import java.util.*;
/**
* In this class, we create two private variables which is a File and
* ArrayList that is a String that will be
* initialized later on in the code.
*/
public class FileManipulator
{
private File inputFile;
private ArrayList <String> words;
/**
* In this method, we are initializing the File and Arraylist and
* having a Scanner reach a file to grab the text
* and insert it into the ArrayList as a String.
* @param fileName is a type String that the user in the other class
* "FileDriver" types in and it reads the file.
* @throws FileNotFoundException if the file does not exist.
*/
public FileManipulator(String fileName) throws FileNotFoundException
{
inputFile = new File(fileName);
words = new ArrayList<String>();
Scanner fileWords = new Scanner(inputFile);
while(fileWords.hasNextLine()) /** This will read the file and
continue to loop as long as there are still more lines left to read
from the file. */
{
String lineScan = fileWords.nextLine(); /** I created a String
called LineScan that will read the whole line of the file as a String
*/
words.add(lineScan); /** Right here, we add whatever the
String LineScan read, into the String ArrayList. (It adds in lines,
not individual words.) */
}
System.out.println(words.size() + " ");
}
/**
* In this method, the goal is to reverse everything that is in the
ArrayList "words". It is flawed at the moment since
* it correctly returns the lines in reverse order, but the words in
each line are not reversed.
*/
public void reverse()
{
for(int i = words.size()-1; i >= 0; i--) /** This for loop reverses
*each of the lines in reverse order. */
{
System.out.println(words.get(i) + " ");
}
for(int i = 0; i < words.size(); i++)
{
//System.out.print(words.get(i) + " ");
}
}
public void removePlurals()
{
for(int i = words.size()-1; i >= 0; i--)
{
String plurals = words.get(i);
if(plurals.endsWith("s"))
{
words.remove(i);
}
System.out.println(words.get(i) + " ");
}
}
public void stutter(int k)
{
k = 3;
for(int i = words.size()-1; i <= words.size()-1; i++)
{
System.out.println(words.get(i));
}
}
}
Токовый выход:
when tweetle beetles fight ,
it's called a tweetle beetle battle .
and when they battle in puddles ,
they are tweetle beetle puddle battles .
and when tweetle beetles battle with paddles in puddles ,
they call them tweetle beetle puddle paddle battles .
and ...
when beetles battle beetles in puddle paddle battles
and the beetle battle puddles are puddles in bottles ...
... they call these tweetle beetle bottle puddle paddle battle muddles .
and ...
when beetles fight these bottle battles with their paddles
and the bottles are on poodles and the poodles are eating noodles ...
... they call these muddle puddle tweetle poodle beetle noodle
bottle paddle battles .
Желаемый выход:
. battles paddle bottle
noodle beetle poodle tweetle puddle muddle these call they ...
... noodles eating are poodles the and poodles on are bottles the and
paddles their with battles bottle these fight beetles when
... and
. muddles battle paddle puddle bottle beetle tweetle these call they ...
... bottles in puddles are puddles battle beetle the and
battles paddle puddle in beetles battle beetles when
... and
. battles paddle puddle beetle tweetle them call they
, puddles in paddles with battle beetles tweetle when and
. battles puddle beetle tweetle are they
, puddles in battle they when and
. battle beetle tweetle a called it's
, fight beetles tweetle when