Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey, I need help with my program. Is running and compiling but the output is wrong. The output supposed to be my secret file. I

Hey,

I need help with my program. Is running and compiling but the output is wrong. The output supposed to be my secret file. I will really appreciate your help! Thank you in advance and happy Monday!

Here is assignment:

Task #2 String.split and the StringBuilder Class

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.

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

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.

Secret text:

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

java program

import java.io.File; import java.io.IOException; import java.util.Scanner; import java.util.StringTokenizer; public class SecretMessage { public static void main(String[] args)throws IOException { File file = new File("secret.txt"); StringBuilder stringBuilder = new StringBuilder(); String str; char ch; int numberOfTokens = 0; if(file.exists()) { Scanner inFile = new Scanner(file); while(inFile.hasNext()) { StringTokenizer line = new StringTokenizer(inFile.next()); numberOfTokens += line.countTokens(); str= line.nextToken(); if(numberOfTokens == 5) { ch = str.toUpperCase().charAt(0); stringBuilder = stringBuilder.append(ch); numberOfTokens =0; } } System.out.println("----Secret Message----"+ stringBuilder); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

3rd Edition

0128012757, 978-0128012758

More Books

Students also viewed these Databases questions