Question
Java help
Java Read a text file that displays every fifth word in a sentence and displays its last alphabet of every word.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code for this question:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFile {

   public static void main(String[] args) throws FileNotFoundException {
       File file = new File("C:\\Users\\Home\\Desktop\\test.txt"); //       Give the input file location here
       Scanner scan = new Scanner(file);
       scan.useDelimiter("\\Z");
       String str = scan.next();
       str=str.replaceAll("\n", " ");
       str=str.replaceAll("\\.", "");
       str=str.replaceAll(",", "");
       String [] words = str.split(" ");
       for (int i=1;i<=words.length;i++){
           // Check for the fifth word
           if(i%5==0){
               // print the fifth word of the sentence and last alphabet of that word
               System.out.println(words[i-1]+"\t"+words[i-1].charAt(words[i-1].length()-1));
           }
       }
   }

}

ScreenShots:

ModifyServic.. SqMatDiagno... Read File.java X » modify.xhtml ModifyContr... ModifyDeleg... 1e import java.io.File; 2 import

Input:

X TCCUTUI W I LL UNIVIILUTILILILU WEU LOSLITY diuun Tim Derd Rivals AGREEME Decltan Ann test - Notepad - File Edit Format Vie

Output:

R Problems @ Javadoc Declaration Search Console X Debug <terminated> Read File [Java Application] C:\Program FilesVava\jre1.8

Add a comment
Know the answer?
Add Answer to:
Java help Java Read a text file that displays every fifth word in a sentence and...
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
  • Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin....

    Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin.” The rules used by Pig Latin are as follows: • If a word begins with a vowel, just as "yay" to the end. For example, "out" is translated into "outyay". • If it begins with a consonant, then we take all consonants before...

  • Create a data file named REVERSE.TXT and type the sentence                        EVERY GOOD BOY & GIRL...

    Create a data file named REVERSE.TXT and type the sentence                        EVERY GOOD BOY & GIRL DOES FINE                                                                                  Write a C++ program that retrieves the phrase stored in the       the data file REVERSE.TXT and immediately displays the phrase    on the screen. Then your program displays the phrase where      each word is reversed. Notice you’re not reversing the entire sentence. The order of the words remains the same. However,    the spelling of each word is reversed! EXAMPLE:...

  • please write a C++ code Problem A: Word Shadow Source file: shadow.cpp f or java, or.cj...

    please write a C++ code Problem A: Word Shadow Source file: shadow.cpp f or java, or.cj Input file: shadow.in in reality, when we read an English word we normally do not read every single letter of that word but rather the word's "shadow" recalls its pronunciation and meaning from our brain. The word's shadow consists of the same number of letters that compose the actual word with first and last letters (of the word) in their original positions while the...

  • (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text...

    (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by white space characters, punctuation marks (, ; . : ?), quotation marks (' "), and parentheses. Count the words in a case-sensitive fashion (e.g., consider Good and good to be the same word). The words must start with a letter. Display the output of words in alphabetical...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

  • In java, write a program with a recursive method which asks the user for a text...

    In java, write a program with a recursive method which asks the user for a text file (verifying that the text file exists and is readable) and opens the file and for each word in the file determines if the word only contains characters and determines if the word is alpha opposite. Now what I mean by alpha opposite is that each letter is the word is opposite from the other letter in the alphabet. For example take the word...

  • Could you guys write an efficient java program to implement BST. Your java program should read...

    Could you guys write an efficient java program to implement BST. Your java program should read words and its corresponding French and store it in a BST. Then read every English word and print its corresponding French by searching in BST. Assume your java program is in xxxxx5.java file (5th java project), where xxxxx is the first 5 characters of your last name. To compile: javac xxxxx5.java To execute: java xxxxx5 < any data file name Your main method should...

  • JAVA help Need help with this part of the code: If the File exists, read from...

    JAVA help Need help with this part of the code: If the File exists, read from the File. Capitalize the first letter in the file and capitalize the 1st letter after every period and correct the spacing by removing any duplicate spaces so that there is only 1 space between words. Write the modified output to a file named HomeworkOutput6-2.txt

  • Write a Java program to read customer details from an input text file corresponding to problem...

    Write a Java program to read customer details from an input text file corresponding to problem under PA07/Q1, Movie Ticketing System The input text file i.e. customers.txt has customer details in the following format: A sample data line is provided below. “John Doe”, 1234, “Movie 1”, 12.99, 1.99, 2.15, 13.99 Customer Name Member ID Movie Name Ticket Cost Total Discount Sales Tax Net Movie Ticket Cost Note: if a customer is non-member, then the corresponding memberID field is empty in...

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