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.
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...
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...
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...
2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in...
Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in English (dictionary), to find the number of misspelled words in a story file called Alcott-little-261.txt. Please note that the dictionary words are all lower-cased and there are no punctuation symbols or numbers. Hence it is important that you eliminate anything other than a-z and A-Z and then lower case story words for valid comparisons against the WordList file. When you read each line of...
Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in...
Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in English (dictionary), to find the number of misspelled words in a story file called Alcott-little-261.txt. Please note that the dictionary words are all lower-cased and there are no punctuation symbols or numbers. Hence it is important that you eliminate anything other than a-z and A-Z and then lower case story words for valid comparisons against the WordList file. When you read each line of...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT