0

This program is meant to see two files located in a particular folder and then merge those two files and create a third file which is does. From the third merged file it is then searching for a keyword such as "test", once it finds that key word it prints out the location and the line of the keyword which is what is somewhat doing. What is happening is when I run the program it stops after the finds the keyword the first time in a line but it will not continue to search that line. So if there is multiple keyword 'test' in the line it will only find the first one and spit back the position and line. I want it to print both or multiple keywords. I think it is because of the IndexOf logic which is causing the issue.

import com.sun.deploy.util.StringUtils;

import java.io.*;
import java.lang.*;
import java.util.Scanner;

public class Concatenate {
    public static void main(String[] args) {
        String sourceFile1Path = "C:/Users/me/Desktop/test1.txt";
        String sourceFile2Path = "C:/Users/me/Desktop/test2.txt";

        String mergedFilePath = "C:/Users/me/Desktop/merged.txt";

        File[] files = new File[2];
        files[0] = new File(sourceFile1Path);
        files[1] = new File(sourceFile2Path);

        File mergedFile = new File(mergedFilePath);

        mergeFiles(files, mergedFile);
        stringSearch(args);
    }

    private static void mergeFiles(File[] files, File mergedFile) {

        FileWriter fstream = null;
        BufferedWriter out = null;
        try {
            fstream = new FileWriter(mergedFile, true);
            out = new BufferedWriter(fstream);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        for (File f : files) {
            System.out.println("merging: " + f.getName());
            FileInputStream fis;
            try {
                fis = new FileInputStream(f);
                BufferedReader in = new BufferedReader(new InputStreamReader(fis));

                String aLine;
                while ((aLine = in.readLine()) != null) {
                    out.write(aLine);
                    out.newLine();
                }

                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void stringSearch(String args[]) {
        try {
            String stringSearch = "test";
            BufferedReader bf = new BufferedReader(new FileReader("C:/Users/me/Desktop/merged.txt"));

            int linecount = 0;
            String line;

            System.out.println("Searching for " + stringSearch + " in file");

            while (( line = bf.readLine()) != null){
                linecount++;
                int indexfound = line.indexOf(stringSearch);


                if (indexfound > -1) {

                    System.out.println(stringSearch + " was found at position " + indexfound + " on line " + linecount);
                    System.out.println(line);

                }

            }

            bf.close();
        }
        catch (IOException e) {
            System.out.println("IO Error Occurred: " + e.toString());
        }


    }

}
2
  • 2
    1) This is Java code, not Javascript. Please remove the Javascript tag. 2) Your question is a little unclear, could you explain a little more? Space it out into a couple sentences instead of one long one please.
    – Mage Xy
    Nov 25, 2015 at 15:57
  • sorry about that i edited it a little bit for better understanding, thanks for the feedback . i am new to this community! this is actually my first post .
    – An0nym0us
    Nov 25, 2015 at 16:28

1 Answer 1

0

It's because you are searching for the word once per line in your while loop. Each iteration of the loop takes you to the next line of the file because you are calling bf.readLine(). Try something like the following. You may have to tweak it but this should get you close.

        while (( line = bf.readLine()) != null){
            linecount++;

            int indexfound = line.indexOf(stringSearch);

            while(indexfound > -1)
            {
                System.out.println(stringSearch + " was found at position " + indexfound + " on line " + linecount);
                System.out.println(line);
                indexfound = line.indexOf(stringSearch, indexfound);
            }
        }
2
  • thanks Josh, but when I implemented that code it when in a infinite loop of printing the first line. any idea why?
    – An0nym0us
    Nov 25, 2015 at 16:23
  • Try stepping through it in debug. I expected that you might have to tweak it. You might need to change it to indexfound=line.indexOf(stringsearch, indexfound+1); Nov 25, 2015 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.