Question

*java question* I have a program that needs to search if “of my” and “there” are...

*java question*
I have a program that needs to search if “of my” and “there” are in a text file but each answer (true or false) has to go into a different output file. if im only allowed to read the text file once is there a way to use scanner to read the text file once and print the answer into two different output files?

Homework Answers

Answer #1
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class CheckInFile {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter input file name: ");
        File file = new File(in.nextLine());
        try {
            Scanner fin = new Scanner(file);
            PrintWriter pw1 = new PrintWriter("ofmy.txt");
            PrintWriter pw2 = new PrintWriter("there.txt");

            boolean ofmy = false, there = false;
            String line;
            while (fin.hasNextLine()) {
                line = fin.nextLine().toLowerCase();
                if (line.contains("of my")) {
                    ofmy = true;
                }
                if (line.contains("there")) {
                    there = true;
                }
            }

            if (ofmy) {
                pw1.println("of my is in " + file.getAbsolutePath());
            } else {
                pw1.println("of my is not in " + file.getAbsolutePath());
            }

            if (there) {
                pw2.println("there is in " + file.getAbsolutePath());
            } else {
                pw2.println("there is not in " + file.getAbsolutePath());
            }

            System.out.println("statuses of my and there have been written to ofmy.txt and there.txt");
            fin.close();
            pw1.close();
            pw2.close();
        } catch (FileNotFoundException e) {
            System.out.println(file.getAbsolutePath() + " is not found!");
        }
        in.close();
    }
}

Know the answer?
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
Similar Questions
I have a question relating to java in the IDE intelij: My professor wants us to...
I have a question relating to java in the IDE intelij: My professor wants us to make a sentiment analysis program in java. he gave us two java files but i do not know how to place them inside the IDE. these are the file names: MainApp.java ReviewClassifier.java how to i open them in the IDE? do i have to create a Project? how do i append them to it? I want to know the general idea of how to...
I have a text file that looks like this: Hello a/m The letter "a" is a...
I have a text file that looks like this: Hello a/m The letter "a" is a command that is supposed to append the letter "m" that is followed by the slash to the end of the word "Hello" How would I write a Java program to read and scan the file, and when the letter "a" is found, it appends "m" to the phrase "Hello"? The output should print "Hellom"
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter feeds. At a high level, your program is going to perform the following: Read in two files containing twitter feeds. Merge the twitter feeds in reverse chronological order (most recent first). Write the merged feeds to an output file. Provide some basic summary information about the files. The names of the files will be passed in to your program via command line arguments. Use...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
This assignment involves using a binary search tree (BST) to keep track of all words in...
This assignment involves using a binary search tree (BST) to keep track of all words in a text document. It produces a cross-reference, or a concordance. This is very much like assignment 4, except that you must use a different data structure. You may use some of the code you wrote for that assignment, such as input parsing, for this one. Remember that in a binary search tree, the value to the left of the root is less than the...
Hi, I need this program written in Java for my intro CS class. Thank you in...
Hi, I need this program written in Java for my intro CS class. Thank you in advance. (also here is the description given in class for the code) Write a program that displays all the leap years, ten per line, from 101 to 2100, separated by exactly one space. Also display the number of leap years in this period. - You should use the method isLeap which returns "true" if the year is a leap year, otherwise it should return...
JAVA: Design and implement a recursive version of a binary search.  Instead of using a loop to...
JAVA: Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are passed to...
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
I NEED A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX, if we have an DB...
I NEED A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX, if we have an DB file has a records of students number and names like this (Student file) stNum**stName 1 jack 2 maya 3 sam 4 alex 5 jane . . . . . . this file may have a thousands records, if we want to search for a specific record(student), we need to find it by (Index file), in this index file we have this (Index file) stNum**Address...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT