Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中,如何将字符串ArrayList中的单词分解,然后将其反转?_Java_File_Class_Arraylist - Fatal编程技术网

在Java中,如何将字符串ArrayList中的单词分解,然后将其反转?

在Java中,如何将字符串ArrayList中的单词分解,然后将其反转?,java,file,class,arraylist,Java,File,Class,Arraylist,标题听起来可能令人困惑 简单地说,我有一个里面有文本的文件。我用扫描仪读了那篇文章。我将该文本存储到一个字符串ArrayList中。我的问题是,我需要使一个方法反转该ArrayList中的文本。文本确实相反,但不是完全相反。我也需要单词的顺序相反,但是我的字符串数组列表在句子中被分解了 因此,我需要帮助访问每个ArrayList,并将这些单词分解为其他的“”,然后将它们放入我的方法中,以便它能够正确地反转这些单词 我想做到的是首先把台词拆开,把字句颠倒过来。最困难的部分仍然是把全文按行返回。我的

标题听起来可能令人困惑

简单地说,我有一个里面有文本的文件。我用扫描仪读了那篇文章。我将该文本存储到一个字符串ArrayList中。我的问题是,我需要使一个方法反转该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));
    }
}
}
所需输出:

. 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
(请举例说明。)

我假设要把单词分开,然后颠倒它们的顺序

在本例中,请尝试使用String.split(“”)创建一个字符数组。 i、 e:

String-sense=“这是我做得很差的答案。”;
String[]words2=句子。拆分(“”);
String[]words=新整数[words.length];

对于(int i=words.length;i如果您想保留句子,但将每个句子中的单词颠倒过来,您可以:

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);

如果您试图按相反顺序获取单词的
列表

问题是您正在将行添加到
列表
,然后反向打印这些行,但不打印单词。您可以通过在空白处拆分行并将所有这些标记添加到
列表
,轻松解决此问题。您可以使用以下方法执行此操作:

但是,我们可以通过以下方式更优雅地实现这一点:

或者,当您打印时,您可以将其拆分为文字:

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);
BufferedReader br=new BufferedReader(new FileReader(fileName));
列表=br.lines()
.map(e->e.split(\\s+))
.flatMap(lineArr->Arrays.stream(lineArr))
.collect(Collectors.toList());
收款。反向(列表);

这将使用该方法获取一系列行,将它们拆分为单词,反转它们,然后将它们收集到
列表中,然后反转它们我找到了它。我在这里获得了一些帮助,但我将其移到了我的反转方法

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) + " ");
    }
 }
公共类文件操纵器
{
私有文件输入文件;
私人ArrayList词;
/**
*在这个方法中,我们初始化文件和Arraylist,并有一个
扫描仪到达一个文件以抓取文本
*并将其作为字符串插入ArrayList。
*@param fileName是另一个类中的用户使用的类型字符串
“FileDriver”输入并读取文件。
*@如果文件不存在,则抛出FileNotFoundException。
*/
公共文件操纵器(字符串文件名)引发FileNotFoundException
{
inputFile=新文件(文件名);
words=newarraylist();
Scanner fileWords=新扫描仪(inputFile);
while(fileWords.hasNextLine())
{
StringBuilder sb=新的StringBuilder();
String[]wordsArr=fileWords.nextLine().split(\\s+);
for(字符串str:wordsArr)
{
插入(0,str)。插入(str.length(),“”);
}
添加(某人的字符串());
}
}
/**
*在这种方法中,目标是反转ArrayList中的所有内容
“文字”。它现在是有缺陷的,因为
*它正确地按相反的顺序返回行,但返回每行中的单词
这是不可逆的。
*/
公共无效反向()
{
for(int i=words.size()-1;i>=0;i--)//此for循环反转
按相反的顺序排列。
{
System.out.println(words.get(i)+”);
}
}

循环只打印出每一行。你需要一个嵌套的循环来打印行中的每一个单词。你知道怎么做吗?@AdanVivero-是的。使用
split()
然后循环从
split()
生成的
数组。好的,我会尝试一下,让你知道发生了什么。我试着放单词。添加(lineScan.split(“\\s+”;它不起作用,因为它是字符串ArrayList。在我的字符串ArrayList中,它是“words”"。它有一个文本文件,可以返回几行字。我将这些行放在一个数组中,每个索引都包含一行字,而不是每个单独的字。我想知道的是如何将这些字拆分并颠倒顺序。谢谢……我假设了一些我不应该有的东西。我很快会再次检查它。此外,评论的目的不是用来显示答案。不是颠倒字母,只是把字符串数组列表中的单词作为句子。所以我想把这些句子拆分成单词,然后颠倒这些单词的顺序。例如“瓶子桨战斗”。我希望它是。战斗桨瓶子就像那样。我把它修好了。它终于满足了你的要求。所以如果你向下滚动,我有一个方法叫reverse。我将在那里执行这个任务。我尝试了你给我的for循环。它确实把单词分开了,但没有按正确的顺序。而且,我需要它们以相反的顺序返回。不仅仅是每个单词。你知道什么你认为呢?我知道了,但我需要在我的反向方法中使用它。如果你向下滚动,你会看到它。我找到了。谢谢你的帮助。我将它转换为我的反向方法,但非常感谢你在上面的帮助。
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]);
    }
}
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);
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) + " ");
    }
 }