简体   繁体   English

反转文件中的每个单词并打印出来

[英]Reversing each word in file and printing it out

I have a file that looks like it:我有一个看起来像这样的文件:

"Gettyburg Address “葛底堡演说
Abraham Lincoln亚伯拉罕·林肯

Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the Fourscore 和七年前,我们的祖先在这片大陆上创造了一个新的国家,在自由中孕育,并致力于
proposition that all men are created equal.人人生而平等的命题。 " "

What I am trying to do is to print the file in the same format how it is but with reversed words.我想要做的是以相同的格式打印文件,但使用颠倒的单词。 Could someone help me with that?有人可以帮我吗?

here is my method:这是我的方法:

public static void convertWord() throws Exception {
    String name;
    int sum = 0;
    
    Scanner sc2 = new Scanner(new File("text.txt"));
    while (sc2.hasNextLine()) {
        name = sc2.nextLine();
        for (int i = name.length() - 1; i >= 0; i--) {
            char c = name.charAt(i);
            System.out.print(c);
            
        }

This is the output: run: sserddA grubytteGnlocniL maharbA siht no htrof thguorb srehtaf ruo oga sraey neves dna erocsruoF eht ot detacided dna,ytrebil ni deviecnoc,noitan wen a tnenitnoc.lauqe detaerc era nem lla taht noitisoporpBUILD SUCCESSFUL (total time: 0 seconds) This is the output: run: sserddA grubytteGnlocniL maharbA siht no htrof thguorb srehtaf ruo oga sraey neves dna erocsruoF eht ot detacided dna,ytrebil ni deviecnoc,noitan wen a tnenitnoc.lauqe detaerc era nem lla taht noitisoporpBUILD SUCCESSFUL (total time: 0 seconds)

Everything is in one line.一切都在一条线上。

Thank you in advance for your help:)预先感谢您的帮助:)

You can reverse using StringBuilder::reverse您可以使用StringBuilder::reverse

public static void convertWord() throws Exception {
    String name;
    Scanner sc2 = new Scanner(new File("text.txt"));
    StringBuilder builder = new StringBuilder();
    while (sc2.hasNextLine()) {
        name = sc2.nextLine();
        builder.append(name);
    }
    String reverse = builder.reverse().toString();
    System.out.println(reverse);
    sc2.close();
}

I guess the problem is more elaborate than just handling the newlines correctly.我想这个问题比正确处理换行符更复杂。 The way I understand it is that only the words themselves need to be reversed, not the order of the words.我理解的方式是只需要反转单词本身,而不是单词的顺序

Example: "Foo bar" should become "ooF rab" , and not "rab ooF" .示例: "Foo bar"应该变成"ooF rab" ,而不是"rab ooF"

And then there may be commas, periods, question marks and other stuff that does not belong to a "word", which should also stay in place.然后可能有逗号,句号,问号和其他不属于“单词”的东西,它们也应该留在原处。 Actually this is the far more tricky part of the problem than the word reversing itself.实际上,这是问题中比反转本身更棘手的部分。

My first shot would be something like this, although I have not tested it, so don't blame me for minor issues.我的第一枪是这样的,虽然我没有测试过,所以不要因为小问题责怪我。 You get the idea:你明白了:

String[] words = wholeContentsOfFile.split("[\n\t .,!?]"); // whatever delimiters you may like
Map<String, String> map = new HashMap<>();
for (String w : words) {
    if (!"".equals(w)) {
        map.put(w, new StringBuilder(w).reverse().toString());
    }
}

String result = s;
for (Map.Entry<String, String> e : map.entrySet()) {
    result = result.replaceAll("\\b" + e.getKey() + "\\b", e.getValue());
}

System.out.println(s);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM