Java 我想读一个文件,同时检查一个单词是否存在于文件中。如果单词存在,我的一个方法将返回+1

Java 我想读一个文件,同时检查一个单词是否存在于文件中。如果单词存在,我的一个方法将返回+1,java,string,comparison,Java,String,Comparison,这是我的密码。我想读取一个名为write.txt的文件,然后一旦它读取。将其与单词进行比较,这里我使用字符串类型的target variableof,一旦在名为findTarget的方法中完成比较,在条件为true后将返回1。我试图调用该方法,但不断出现错误。test.java:88:错误:找不到符号 字符串测试=findTargettarget1,source1; ^ 符号:变量target1 地点:班级考试 1错误 有人能纠正我的错误吗。我对编程很陌生 import java.util.*;

这是我的密码。我想读取一个名为write.txt的文件,然后一旦它读取。将其与单词进行比较,这里我使用字符串类型的target variableof,一旦在名为findTarget的方法中完成比较,在条件为true后将返回1。我试图调用该方法,但不断出现错误。test.java:88:错误:找不到符号 字符串测试=findTargettarget1,source1; ^ 符号:变量target1 地点:班级考试 1错误 有人能纠正我的错误吗。我对编程很陌生

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);


}

}
错误是因为findTarget是类函数

那么,你有这个:

test ob = new test();

String testing = findTarget(target1, source1);
…应更改为从静态上下文调用函数:

//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.
我没有您的文件内容来进行试运行,但这至少有助于克服错误

===== 更新:

你接近了

在main中,更改循环中的代码,使其如下所示:

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);

我终于能够解决我的问题了。但是扩展功能。我想把加法增加1。但在我的编程中,它不断地给我输出

答案是:1答案是:1

相反,我希望我的程序打印的不是两个1,而是1+1=2

有人能解决这个递增问题吗

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;


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

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

        int add = 0;

        // this function checks the character whether it is present.


        for (int i = 0; i < source_len; ++i) // i is a 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
            Scanner sc = new Scanner(System.in);

            System.out.println("Enter your review: ");
            String source1 = sc.nextLine();

            //String source1 = "Searching for a string within a string the hard way.";
            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);


                int testing = test.findTarget(target1, source1);
                System.out.println("answer is: "+testing);
                //target1 = br.readLine();
            }

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

检查target1的定义是否在其使用范围内。换言之,在同一个封闭的{}代码块中。我更新了我的答案,以了解您询问的最新更改。。。请参阅底部附近的更新部分。我按照您的指示进行了尝试,但仍然收到相同的错误!有没有办法把我的代码和我的问题一起附上?我是StackOverflow新手。无法附加,但您可以更新问题并用您现在拥有的最新代码替换代码。