We will need to use a variety of Character class and String class methods to  validate the data and separate it in order to process it. We will also use a Character class method to allow the user to continue the program if desired.  The String class’s split method will allow us to process the tokens in a text file in  order to decode a secret message. We will use the first letter of every 5th token read in  from a file to reveal the secret message.  Task #2 String.split and the StringBuilder Class  1. Copy the file secret.txt (Code Listing 9.3) from the Student Files 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.  Code Listing 9.3 (secret.txt) January is the first month and December is the last.  Violet is a purple color as are lilac and plum.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

We will need to use a variety of Character class and String class methods to 
validate the data and separate it in order to process it. We will also use a Character
class method to allow the user to continue the program if desired. 
The String class’s split method will allow us to process the tokens in a text file in 
order to decode a secret message. We will use the first letter of every 5th token read in 
from a file to reveal the secret message. 

Task #2 String.split and the StringBuilder Class 

1. Copy the file secret.txt (Code Listing 9.3) from the Student Files 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. 

Code Listing 9.3 (secret.txt)
January is the first month and December is the last. 
Violet is a purple color as are lilac and plum.

terminate
Java Appi
I\.p²\poor\plugins\ g.eci
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method append(char) is undefined for the type StringBuilder
at StringBuilder.main(StringBuilder.java:20)
Transcribed Image Text:terminate Java Appi I\.p²\poor\plugins\ g.eci Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method append(char) is undefined for the type StringBuilder at StringBuilder.main(StringBuilder.java:20)
import java.io.File;
2 import java.io.FileNotFoundException;
3 import java.util.Scanner;
4
public class StringBuilder {
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3}
public static void main(String[] args) {
// Step 1: Read the file and split it into word tokens
String filename
"secret (2).txt";
String fileContents
String[] tokens
}
}
}
}
=
// Step 2: Process the tokens to extract the secret message
StringBuilder secretMessage new StringBuilder();
for (int i
=
=
readFile(filename);
fileContents.split("\\s+");
0; i < tokens.length; i += 5) {
if (i < tokens.length) { // make sure we don't go out of bounds
String word
tokens[i];
char firstLetter
}
=
=
word.charAt(0);
secretMessage.append(Character.toUpperCase(firstLetter));
// Step 3: Print the secret message
System.out.println("The secret message is: + secretMessage.toString());
private static String readFile(String filename) {
StringBuilder contents
new StringBuilder();
try {
scanner.close();
System.exit(1);
=
=
=
Scanner scanner new Scanner(new File(filename));
while (scanner.hasNextLine()) {
scanner.nextLine();
=
String line
contents.append(line);
=
} catch (FileNotFoundException
System.err.println("Error:
"1
}
return contents.toString();
e) {
File not found: + filename);
Transcribed Image Text:import java.io.File; 2 import java.io.FileNotFoundException; 3 import java.util.Scanner; 4 public class StringBuilder { 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3} public static void main(String[] args) { // Step 1: Read the file and split it into word tokens String filename "secret (2).txt"; String fileContents String[] tokens } } } } = // Step 2: Process the tokens to extract the secret message StringBuilder secretMessage new StringBuilder(); for (int i = = readFile(filename); fileContents.split("\\s+"); 0; i < tokens.length; i += 5) { if (i < tokens.length) { // make sure we don't go out of bounds String word tokens[i]; char firstLetter } = = word.charAt(0); secretMessage.append(Character.toUpperCase(firstLetter)); // Step 3: Print the secret message System.out.println("The secret message is: + secretMessage.toString()); private static String readFile(String filename) { StringBuilder contents new StringBuilder(); try { scanner.close(); System.exit(1); = = = Scanner scanner new Scanner(new File(filename)); while (scanner.hasNextLine()) { scanner.nextLine(); = String line contents.append(line); = } catch (FileNotFoundException System.err.println("Error: "1 } return contents.toString(); e) { File not found: + filename);
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education