【发布时间】: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 感谢您编辑我的问题。