Java 我如何根据索引旋转句子中的每个单词?

Java 我如何根据索引旋转句子中的每个单词?,java,Java,我正在读一个输入文件,其中新行包含两个句子。我必须编写一个代码,将句子中的每个单词向右旋转,然后写入output.txt文件。例如,input.txt文件包含以下内容: Hello World. Welcome to java programming. 假设“Hello”有索引1。它应该向右旋转一个字符,即oHell。“World.”有索引2,它应该向右旋转2个字符,即ldWor.,保持句点(.) 在下一行,索引再次以1开始。i、 e.索引为1的“欢迎”应向右旋转1个字符,索引为2的“至”应向

我正在读一个输入文件,其中新行包含两个句子。我必须编写一个代码,将句子中的每个单词向右旋转,然后写入output.txt文件。例如,input.txt文件包含以下内容:

Hello World.
Welcome to java programming.
假设“Hello”有索引1。它应该向右旋转一个字符,即oHell。“World.”有索引2,它应该向右旋转2个字符,即ldWor.,保持句点(.)

在下一行,索引再次以1开始。i、 e.索引为1的“欢迎”应向右旋转1个字符,索引为2的“至”应向右旋转2个字符,索引为3的“java”应向右旋转3个字符,索引为4的“编程”应向右旋转4个字符

因此,输出应为:

oHell ldWor.
eWelcom to avaj mingprogram.
到目前为止,我已经成功地读取了文件并将单词存储在数组列表中。而且,我被旋转逻辑所困扰。下面是我的代码:

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

        FileInputStream inputFile = new FileInputStream("input.txt");
        FileWriter outputFile = new FileWriter("output.txt");
        Scanner scanner = new Scanner(inputFile);
        //System.out.println("Enter a Sentence");

        while(scanner.hasNextLine()) {
            String str = scanner.nextLine();
            String str1[] = str.split(" ");
            ArrayList<String> words = new ArrayList<String>(Arrays.asList(str1));
            for(int i = 0; i < words.size(); i++) {
                System.out.print((i+1)+":"+words.get(i)+"\n");
                outputFile.write((i+1)+"\t");
                outputFile.write(words.get(i)+"\n");
            }
        }
        inputFile.close();
        outputFile.close();
        scanner.close();
     }
publicstaticvoidmain(字符串[]args)引发IOException{
FileInputStream inputFile=新的FileInputStream(“input.txt”);
FileWriter outputFile=新的FileWriter(“output.txt”);
扫描仪=新扫描仪(输入文件);
//System.out.println(“输入句子”);
while(scanner.hasNextLine()){
String str=scanner.nextLine();
字符串str1[]=str.split(“”);
ArrayList words=新的ArrayList(Arrays.asList(str1));
for(int i=0;i
在这里,我只是想将数组列表打印到输出文件,看看是否能够将其写入文件


有人能帮忙吗?谢谢。

如果您使用的是java 8或更高版本,这将解决您的问题

    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;

    public class Test {
        public static void main(String[] args) throws IOException {
            rotate();
        }

        private static void rotate() throws IOException {
            FileInputStream inputFile = new FileInputStream("input.txt");
            FileWriter outputFile = new FileWriter("output.txt");
            Scanner scanner = new Scanner(inputFile);

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String last = "";
                if(line.charAt(line.length()-1) == '.'){
                    last = ".";
                    line = line.substring(0,line.length()-1);
                }
                String str[] = line.split(" ");
                outputFile.write(IntStream.range(0, str.length).mapToObj(i -> rotate(str[i], i+1)).collect(Collectors.joining(" ")) +last+ "\n");
            }
            inputFile.close();
            outputFile.close();
            scanner.close();
        }

        private static String rotate(String str, int position) {
            return str.substring(str.length()-position)+str.substring(0,str.length()-position);
        }
    }

而实际产出是<代码>c.toString()
不打印任何内容,因为它返回一个
字符串
,您需要调用
System.out.println(c)
-这将为您调用
toString()
(因此您可以忽略该选项)。与
cc.toString()相同-基本上,如果你想
test()
打印一些东西,你必须打印一些东西。另外,在列表中创建一堆汽车,然后在
test
中完全忽略它们并创建一堆新的汽车,这有什么意义?如果你能将其简化为。我知道说起来容易做起来难。当我们只看到程序的一半时,也很难判断程序中包含了什么。您的实现可能很好。正如@ElliottFrisch所说,您只需要测试来打印一些东西。它会抛出一个StringIndexOutOfBoundException。通过修改outputFile.write(IntStream.range(0,str.length-1)。mapToObj(i->rotate(str[i],i)).collector(Collectors.joining(“”)+last+“\n”);我现在得到的输出:你好。欢迎ot vaja。它跳过了两个句子的最后一个单词。而且,第一个词也不是rotate@Coder过去几天我都不在家。所以我无法回答。我只测试了您提供的示例数据的代码。有了这段代码,您可以获得主要思想,并对其进行改进,使之适合您的需求。如果你能解决你的问题,我很高兴。:)