【问题标题】:Reverse sentences, lines and words of a file反转文件的句子、行和单词
【发布时间】:2014-03-19 04:07:09
【问题描述】:

我正在尝试反转文件中的单词和行,但我的代码仅反转单词而不保留每个句子的格式并打印新行。

这是文件的示例以及输出的外观。

reverse.txt(实际):

He looked for a book.
He picked up the book.
He read the book.
He liked the book.

预期结果:

book. the liked He
book. the read He
book. the up picked He
book. a for looked He

这是我目前的 JAVA 代码

import java.io.*;
import java.util.*;

public class ReverseOrder {

  public static void main (String[] args)
        throws FileNotFoundException {

    ArrayList<String> revFile = new ArrayList<String>();

    Scanner input = new Scanner(new File("reverse.txt"));
        while (input.hasNext()){
            revFile.add(input.next());
    for(int i = revFile.size()-1; i >= 0; i--){
        System.out.println(revFile.get(i) + " ");   
    }
    }
}
}

【问题讨论】:

  • 也许您应该尝试在堆栈中添加和删除令牌
  • 已编辑问题,以免其他人感到困惑,谢谢@Leo
  • 你可以试试下面这个答案。
  • @jubinPatel 感谢您编辑我的问题。

标签: java arraylist reverse


【解决方案1】:

你可以试试这个:-

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class ReverseOrder {

    /**
     * @param args
     */
    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub
        ArrayList<String> revFile = new ArrayList<String>();

        Scanner input = new Scanner(new File("/reverse.txt"));
            while (input.hasNextLine()){
                revFile.add(input.nextLine()); 
            }
            for(int i = (revFile.size()-1); i >=0 ; i--){
                String ar[]=revFile.get(i).split(" ");

                for(int j = (ar.length-1); j >=0; j--){
                    System.out.print(ar[j] + " "); 
                }
                System.out.println(" ");
                ar=null;
            }


        }
}

输出:-

  book. the liked He  
  book. the read He  
  book. the up picked He  
  book. a for looked He

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    我的尤达式建议

        public static void main(String[] args) {
            String s = "I want cake today.";
            String[] ss = s.split(" ");
            Stack<String> sss = new Stack<>(); 
            for(String ssss:ss){
                sss.push(ssss);
            }
    
            while(!sss.isEmpty()){
                System.out.print(sss.pop());
                System.out.print(" ");
            }
        }
    

    给予

    today. cake want I 
    

    对于行,对另一个堆栈执行相同操作。

    或者,如果您不想使用堆栈,请将标记和行存储到一些 ArrayList 中并使用 Collections.reverse(list)

    【讨论】:

    • 换行怎么样?确实有多行。
    • 不是代码,问题是指一个文件有多行。你只是给了一个字符串的解决方案。
    • @Ruben 对我来说看起来很聪明。我想他已经知道如何处理了。
    • 我想反转单词和行,就像我上面放的输出示例一样。我编辑是因为我写错了。
    • 将行存储到 ArrayList 并使用 Collections.reverse(list)
    【解决方案3】:

    你可以使用

      org.apache.commons.lang.StringUtils;
    

    它有广泛的与字符串相关的方法。为了您的目的,您可以使用: StringUtils.reverseDelimited(str, ' ') 函数如下:

     while (input.hasNext()) {
            String str = input.nextLine();
            System.out.println(str);
            System.out.println(StringUtils.reverseDelimited(str, ' '));// <- reverse line 
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      相关资源
      最近更新 更多