如何在java中分解字符串数组列表中的单词,然后反转它们?

rseugnpd  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(367)

这个标题听起来可能令人困惑。
简单地说,我有一个文件里面有文本。我用扫描器看了那篇文章。我将文本存储到一个字符串数组列表中。我的问题是,我需要使一个方法反转该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
iyfjxgzm

iyfjxgzm1#

如果你想保留句子,但要把每个句子中的单词颠倒过来,你可以这样做:

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 ,然后反转

rks48beu

rks48beu2#

我想出来了。我在这里得到了一些帮助,但我把它移到了我的反向方法。

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) + " ");
    }
 }
rkue9o1l

rkue9o1l3#

(请举例说明。)
我想把这些词分开,然后颠倒它们的顺序。
在这种情况下,请尝试使用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库中可能还有其他方法可以满足您的要求。

相关问题