Search code examples
javastringcomparison

I want to read a file and also check a word whether the word is present in the file or not. If the word is present one of my method will return +1


This is my code. I want to read a file called "write.txt" and then once it reads. Compare it with a word, here I use "target variable(of string type) once the comparison is done inside the method called findTarget it will return 1 after the condition is true. I try to call the method but I keep getting an error. test.java:88: error: cannot find symbol String testing = findTarget(target1, source1); ^ symbol: variable target1 location: class test 1 error can someone correct my mistake. I am quite new to programming.

import java.util.*;
import java.io.*;


public class test {

public static int findTarget( String target, String source ) 
{

int target_len = target.length();
int source_len = source.length();

int add = 0;

for(int i = 0;i < source_len; ++i) // i is an varialbe used to count upto 
source_len.
{
int j = 0; // take another variable to count loops        
    while(add == 0)
    {
        if( j >= target_len ) // count upto target length
        {
            break;
        }
        else if( target.charAt( j ) != source.charAt( i + j ) ) 
        {
            break;
        } 
        else 
        {
            ++j;
            if( j == target_len ) 
            {     
            add++; // this will return 1: true

            }
        }
    }
}
return add;
//System.out.println(""+add);
}
public static void main ( String ... args ) 
{
//String target = "for";
// function 1    
try
{
// read the file
File file = new File("write.txt"); //establising a file object
BufferedReader br = new BufferedReader(new FileReader(file));   
//reading the files from the file object "file"

String target1; 
while ((target1 = br.readLine()) != null) //as long the condition is not null it will keep printing.
System.out.println(target1);

//target.close();
}
catch (IOException e)
  {
     System.out.println("file error!"); 
  }

String source1 = "Searching for a string within a string the hard way.";


// function 2

test ob = new test();

String testing = findTarget(target1, source1);


// end    
//System.out.println(findTarget(target, source));
System.out.println("the answer is: "+testing);


}

}

Solution

  • The error is because findTarget is a class function.

    So, where you have this:

    test ob = new test();
    
    String testing = findTarget(target1, source1);
    

    ...should be changed to call the function from a static context:

    //test ob = new test();  not needed, the function is static
    
    int testing = test.findTarget(target1, source1);
    // also changed the testing type from String to int, as int IS findTarget's return type.
    

    I don't have your file contents to give a trial run, but that should at least help get past the error.

    ===== UPDATE:

    You are close!

    Inside main, change the code at your loop so that it looks like this:

    String target1;
    int testing = 0;  // move and initialize testing here
    
    while ((target1 = br.readLine()) != null) //as long the condition is not null it will keep printing.
    {
        //System.out.println(target1);
    
        testing += test.findTarget(target1, source1);
        //target1 = br.readLine();
    }
    
    System.out.println("answer is: "+testing);