Please write a java code put the secret file in a text file

Please write a java code

(put the secret file in a text file) the secret file is :January is the first month and December is the last. Violet is a purple color as are lilac and plum.

Process a text file in order to decode a secret message. We will use the first letter of every 5th token starting with the first word read in from a file to reveal the secret message.

1-Copy the file secret.txt. This file is only one line long. It contains 2 sentences.(January is the first month and December is the last. Violet is a purple color as are lilac and plum).

2-Write a main method that will read the file secret.txt, separate it into word tokens.

3-You should process the tokens by taking the first letter of every fifth word, starting with the first word in the file. These letters should be converted to capitals, then be appended to a StringBuilder object to form a word which will be printed to the console to display the

Solution

Please follow the code and comments for description :

CODE :

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

public class FileDecoder { // class to run the code

    public static void main(String[] args) throws FileNotFoundException, IOException { // driver method with expected throws clauses
        Scanner sc = new Scanner(System.in); // scanner class that reads the data from the user
        System.out.println(\"Please Enter the FileName : \"); // prompt for the user to enter the filename
        String filename = sc.nextLine();
        File file = new File(filename + \".txt\"); // opening the file based on the user
        Scanner secretFile = new Scanner(file);

        String line = null; // temporary variable

        line = secretFile.nextLine(); // stores the line into string
        StringBuilder str = new StringBuilder(); // string builder to append the secret letters

        String[] tokens = line.split(\" \"); // eliminating the whitespaces in the data

        for (int i = 0; i < tokens.length; i++) { // iterating over the length of the tokens
            if (((i + 1) % 5) == 0) { // checking for the fifth word in the data
                tokens[i] = Character.toString(tokens[i].charAt(0)).toUpperCase() + tokens[i].substring(1); // converting the first letter to uppercase of every fifth word
                str.append(tokens[i].charAt(0)); // appending the letter to the string builder
            }
        }
        System.out.println(\"So the Secret Code is : \"+str); // printing the secret code to console
    }
}


secret.txt :

January is the first month and December is the last. Violet is a purple color as are lilac and plum.


OUTPUT :

Please Enter the FileName :
secret
So the Secret Code is : MLCP


Hope this is helpful.

Please write a java code (put the secret file in a text file) the secret file is :January is the first month and December is the last. Violet is a purple color
Please write a java code (put the secret file in a text file) the secret file is :January is the first month and December is the last. Violet is a purple color

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site