user9043303
user9043303

Reputation:

How do i rotate every word in a sentence according to their indexes?

I am trying to read an input file containing two sentences on new line. I have to write a code to rotate every word in a sentence to the right side and write to output.txt file. For example, input.txt file contains following:

Hello World.
Welcome to java programming.

Let's say "Hello" has index 1. It should rotate to the right by one character i.e. oHell. "World." has index 2, it should rotate to the right by 2 characters i.e. ldWor., maintaining the position of period(.)

And on the next line index again begins with 1. i.e. "Welcome" with index 1 should rotate to the right by 1 character, "to" with index 2 should rotate to the right by 2 characters, "java" with index 3 should rotate to the right by 3 characters, "programming" with index 4 should rotate to the right by 4 characters.

So, the output should be:

oHell ldWor.
eWelcom to avaj mingprogram.

So far, I have managed to read the file and store the words in array list. And, I am stuck with rotation logic. Below is my code:

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

Here, I am just trying to print the array list to output file to see if I am able to write it to file.

Can anyone help? Thanks.

Upvotes: 1

Views: 218

Answers (1)

janith1024
janith1024

Reputation: 1042

If you are using java 8 or higher this would solve your problem.

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

Upvotes: 2

Related Questions