标题听起来可能令人困惑。
简单来说,我有一个文件,其中包含文本。我使用扫描仪读取该文本。我将该文本存储到一个String的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
答案 0 :(得分:1)
如果您想保留句子,但每个句子中的单词都颠倒了,您可以这样做:
while(fileWords.hasNextLine()) {
StringBuilder sb = new StringBuilder();
String[] wordsArr = fileWords.nextLine().split("\\s+");
for(String str: wordsArr) {
sb.insert(0, str).insert(str.length(), " ");
}
words.add(sb.toString());
}
Collections.reverse(words);
words.forEach(System.out::println);
如果您试图以相反的顺序获得List
个单词:
问题是您要向List
添加行,然后反向打印这些行,而不是单词。您可以通过在空白处分割行并将所有这些标记添加到List
中来轻松解决此问题。您可以使用split()
方法:
for(String str : lineScan.split("\\s+")) {
words.add(str);
}
不过,我们可以使用Collections.addAll
更优雅地做到这一点:
Collections.addAll(words, lineScan.split("\\s+"));
或者在打印时,可以在其中将其拆分为单词:
for(int i = words.size()-1; i >= 0; i--)
{
String[] wordArr = words.get(i).split("\\s+");
for(int j = wordArr.length - 1; j >= 0; j--) {
System.out.println(wordArr[j]);
}
}
或Java 8 +:
BufferedReader br = new BufferedReader(new FileReader(fileName));
List<String> list = br.lines()
.map(e -> e.split("\\s+"))
.flatMap(lineArr -> Arrays.stream(lineArr))
.collect(Collectors.toList());
Collections.reverse(list);
哪种方法将使用lines()
方法来获取一行流,将它们拆分为单词,将它们反转,然后将它们收集到List
,然后反转它们
答案 1 :(得分:0)
我知道了。我在这里得到了一些帮助,但是我将其移至我的反向方法。
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())
{
StringBuilder sb = new StringBuilder();
String[] wordsArr = fileWords.nextLine().split("\\s+");
for(String str: wordsArr)
{
sb.insert(0, str).insert(str.length(), " ");
}
words.add(sb.toString());
}
}
/**
* 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) + " ");
}
}
答案 2 :(得分:-1)
(请举个例子。)
我假设要拆分单词,然后颠倒它们的顺序。
在这种情况下,请尝试使用String.split(“”)创建一个char数组。 即:
String sentence = "This is my very poorly made answer.";
String [] words2 = sentence.split(" ");
String [] words = new int [words.length];
for(int i=words.length; i<=0; i--){
words[words.length - i] = words2[i];
}
此外,java库中还内置了其他方法,可以满足您的要求。