1. Copy the file secret.txt into a path that you can access. Read FilePath.doc i
ID: 3545540 • Letter: 1
Question
1. Copy the file secret.txt into a path that you can access. Read FilePath.doc if you have
questions on file path. Copy SecretMessage.java into your NetBeans or other IDE
tools.
2. Finish the 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. These letters should converted to capitals, then
be appended to StringBuffer object to form a word which will be printed to the
console to display the secret message.
secretmessage.java
import java.io.*;
import java.util.StringTokenizer;
public class SecretMessage
{
public static void main (String [] args)throws IOException
{
FileReader file = new FileReader ("secret.txt");
BufferedReader input = new BufferedReader (file);
StringTokenizer tokenizer;
String fileContents;
String wordToken;
int count = 0;
char letter;
StringBuffer strbuff = new StringBuffer();
// To do - Task #2 step 2
// read one line of the file into fileContents
tokenizer = new StringTokenizer (fileContents);
while (tokenizer.hasMoreTokens())
{
// To do - Task #2 step 2
// get nextToken of tokenizer and assign it to workToken
wordToken = tokenizer.nextToken();
if (count%5 ==0)
{
// To do - Task #2 step 2
// get first letter of the wordToken
// Change it to upper case
// appent it to strbuff
}
count++;
}
System.out.println(strbuff);
}
}
Text file
January is the first month and december is the last. Violet is a purple color as are lilac and plum.
Explanation / Answer
The scret answer is JAVA, here is the code:
import java.io.*;
import java.util.StringTokenizer;
public class SecretMessage
{
public static void main(String[] args) throws IOException
{
FileReader file = new FileReader("secret.txt");
BufferedReader input = new BufferedReader(file);
StringTokenizer tokenizer;
String fileContents;
String wordToken;
int count = 0;
char letter;
StringBuffer strbuff = new StringBuffer();
// To do - Task #2 step 2
// read one line of the file into fileContents
fileContents = input.readLine();
while (fileContents != null)
{
tokenizer = new StringTokenizer(fileContents);
while (tokenizer.hasMoreTokens())
{
// To do - Task #2 step 2
// get nextToken of tokenizer and assign it to workToken
wordToken = tokenizer.nextToken();
if (count % 5 == 0)
{
// To do - Task #2 step 2
// get first letter of the wordToken
// Change it to upper case
// appent it to strbuff
char first = Character.toUpperCase(wordToken.charAt(0));
strbuff.append(first);
}
count++;
}
fileContents = input.readLine();
}
System.out.println(strbuff);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.