Question

In: Computer Science

Write a Java program to read in words from the given file “word.txt”. a. Prompt the...

Write a Java program to read in words from the given file “word.txt”.
a. Prompt the user for two words
b. Print out how many words in the file fall between those words
c. If one of the two words is not contained in the file, print out which word is not found in
the file
d. If both words are not found in the file, print out a message
e. Sample output:
Please type in two words: hello computer
There are 123 words between hello and computer
Please type in two words: good night
good is not found!
Please type in two words: happy day
Both words are not found!

content in word.txt

Punctuation is in there. its 300 pages so cant post it

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!

===========================================================================

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

public class WordsInBetween {

    public static void main(String[] args) {

        String fileName = "words.txt";
        int firstWordPosition = -1;
        int secondWordPosition = -1;

        try {
            Scanner fileReader = new Scanner(new File(fileName));
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Please type in two words: ");
            String firstWord = keyboard.next();
            String secondtWord = keyboard.next();
            String wordRead;
            int wordCount = 0;
            while (fileReader.hasNext()) {
                wordRead = fileReader.next().toLowerCase();
                wordCount += 1;
                if (wordRead.contains(firstWord.toLowerCase())) {
                    firstWordPosition = wordCount;
                } else if (wordRead.contains(secondtWord.toLowerCase())) {
                    secondWordPosition = wordCount;
                    if (firstWordPosition != -1) {

                        break;
                    }
                }
            }
            fileReader.close();
            if (firstWordPosition == -1 && secondWordPosition == -1) {
                System.out.println("Both words are not found!");
            } else if (firstWordPosition == -1) {
                System.out.println(firstWord + " is not found.");
            } else if (secondWordPosition == -1) {
                System.out.println(secondtWord + " is not found.");
            } else {
                System.out.println("There are " + (secondWordPosition - firstWordPosition - 1) + " words between " +
                        firstWord + " and " + secondtWord);
            }

        } catch (FileNotFoundException e) {
            System.out.println("Error: Could not open/read file: " + fileName);
            return;
        }
    }
}

===========================================================================


Related Solutions

Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a Java program to read a set of integers from a file, dataX, and a...
Write a Java program to read a set of integers from a file, dataX, and a set of ranges from a second file, rangeX, and, for each range [a, b] in rangeX, report the SUM of all the integers in dataX which are in the range [a, b]. As the integers are read from file dataX, insert them in a binary search tree. After all the integers have been inserted into the binary search tree, read the ranges from file...
Step by step in python please Write a program this will read a file (prompt for...
Step by step in python please Write a program this will read a file (prompt for name) containing a series of numbers (one number per line), where each number represents the radii of different circles. Have your program output a file (prompt for name) containing a table listing: the number of the circle (the order in the file) the radius of the circle the circumference the area of the circle the diameter of the circle Use different functions to calculate...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Write a python program that does the following: Prompt for a file name of text words....
Write a python program that does the following: Prompt for a file name of text words. Words can be on many lines with multiple words per line. Read the file and convert the words to a list. Call a function you created called list_to_once_words(), that takes a list as an argument and returns a list that contains only words that occurred once in the file. Print the results of the function with an appropriate description. Think about everything you must...
Write a java program that will read a file called stateinfo.txt and will store the information...
Write a java program that will read a file called stateinfo.txt and will store the information of the file into a map. The stateinfo.txt file contains the names of some states and their capitals. The format of stateinfo.txt file is as follows. State                Capital ---------                         ----------- NSW               Sydney VIC                 Melbourne WA                 Perth TAS                 Tasmania QLD                Brisbane SA                   Adelaide The program then prompts the user to enter the name of a state. Upon receiving the user input, the program should...
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
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT