Question

1. Copy the file secret.txt into a path that you can access. Read FilePath.doc if you...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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);

}

}



Add a comment
Answer #2

import java.io.*;

import java.util.StringTokenizer;


public class SecretMessage {

public static void main(String[] args) throws IOException {

FileReader file = new FileReader("/Users/seshu/secret.txt");

@SuppressWarnings("resource")

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

while ((fileContents = input.readLine()) != 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

letter = wordToken.charAt(0);

strbuff.append(Character.toUpperCase(letter));

}

count++;

}

}

System.out.println(strbuff);

}


}



media%2Ffb9%2Ffb930b09-c2a5-4b62-9dc1-ab

Add a comment
Know the answer?
Add Answer to:
1. Copy the file secret.txt into a path that you can access. Read FilePath.doc if you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • Please help me fix my errors. I would like to read and write the text file...

    Please help me fix my errors. I would like to read and write the text file in java. my function part do not have errors. below is my code import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.FileWriter; import java.io.IOException; public class LinkedList {    Node head;    class Node    {        int data;        Node next;       Node(int d)        {            data = d;            next = null;        }    }    void printMiddle()    {        Node slow_ptr...

  • Java Programming Task #2 String.split and the StringBuilder Class 1. Copy the file secret.txt (Code Listing...

    Java Programming Task #2 String.split and the StringBuilder Class 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...

  • Ask the user for the name of a file and a word. Using the FileStats class,...

    Ask the user for the name of a file and a word. Using the FileStats class, show how many lines the file has and how many lines contain the text. Standard Input                 Files in the same directory romeo-and-juliet.txt the romeo-and-juliet.txt Required Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 1137 line(s) contain "the"\n Your Program's Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 553 line(s) contain "the"\n (Your output is too short.) My...

  • Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this...

    Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this file, include your assignment documentation code block. After the documentation block, you will need several import statements. import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; Next, declare the Lab12 class and define the main function. public class Lab12 { public static void main (String [] args) { Step 2: Declaring Variables For this section of the lab, you will need to declare...

  • package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 {    public...

    package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 {    public static void main (String [] args)    {    // ============================================================    // Step 2. Declaring Variables You Need    // These constants are used to define 2D array and loop conditions    final int NUM_ROWS = 4;    final int NUM_COLS = 3;            String filename = "Input.txt";    // A String variable used to save the lines read from input...

  • I have my assignment printing, but I can find why the flowers is printing null and...

    I have my assignment printing, but I can find why the flowers is printing null and not the type of flower. Instructions Make sure the source code file named Flowers.java is open. Declare the variables you will need. Write the Java statements that will open the input file, flowers.dat, for reading. Write a while loop to read the input until EOF is reached. In the body of the loop, print the name of each flower and where it can be...

  • (Reading & Writing Business Objects) I need to have my Classes be able to talk to...

    (Reading & Writing Business Objects) I need to have my Classes be able to talk to Files. How do I make it such that I can look in a File for an account number and I am able to pull up all the details? The file should be delimited by colons (":"). The Code for testing 'Select' that goes in main is: Account a1 = new Account(); a1.select(“90001”); a1.display(); Below is what it should look like for accounts 90000:3003:SAV:8855.90 &...

  • I'm working with Java and I have a error message  1 error Error: Could not find or...

    I'm working with Java and I have a error message  1 error Error: Could not find or load main class Flowers. This is the problem: Instructions Make sure the source code file named Flowers.java is open. Declare the variables you will need. Write the Java statements that will open the input file, flowers.dat, for reading. Write a while loop to read the input until EOF is reached. In the body of the loop, print the name of each flower and where...

  • Can you help me to link two frames in Java Swing and then if you click...

    Can you help me to link two frames in Java Swing and then if you click "Proceed button" it will link to the Second Frame...Thank you :) First frame code: //First import javax.swing.*; import java.awt.event.*; import java.io.*; public class Sample extends JFrame implements ActionListener { JMenuBar mb; JMenu file; JMenuItem open; JTextArea ta; JButton proceed= new JButton("Proceed"); Sample() { open = new JMenuItem("Open File"); open.addActionListener(this); file = new JMenu("File"); file.add(open); mb = new JMenuBar(); mb.setBounds(0, 0, 400, 20); mb.add(file); ta...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT