1. Copy the file secret.txt (Code Listing 9.3) from the Student CD or as directed by your instructor. This file is only one line long. It contains 2 sentences. 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. Convert these letters to uppercase and append them to a StringBuilder object to form a word which will be printed to the console to display the secret message.

Respuesta :

Answer:

In Java:

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class FP {

 public static void main(String[] args) {

   try {

     File myObj = new File("secret.txt");

     Scanner scr= new Scanner(myObj);

     while (scr.hasNextLine()) {

       String data = scr.nextLine();        

       String[] tokens = data.split(" ");  

       for(int i =0;i<tokens.length;i+=5){

        tokens[i] = tokens[i].substring(0, 1).toUpperCase() + tokens[i].substring(1);        }

       StringBuilder newstring = null;

       newstring = new StringBuilder();  

        for(String abc:tokens){newstring.append(abc+" ");     }  

         System.out.print(newstring);      }

     scr.close();      

   } catch(Exception ex){

     System.out.println("An error occurred.");    }

 }

}

Explanation:

See attachment for complete program where comments were used to explain each line

Ver imagen MrRoyal