猿问

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

这是我的代码。我想读取一个名为“write.txt”的文件,然后读取一次。将它与一个单词进行比较,这里我使用“目标变量(字符串类型),一旦在名为findTarget的方法中完成比较,它将在条件为真后返回1。我尝试调用该方法,但我一直收到错误消息。test.java:88: error: cannot find symbol String testing = findTarget(target1, source1); ^ symbol: variable target1 location: class test 1 error 谁能 纠正我的错误。我对编程很陌生。


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



}


}


慕妹3146593
浏览 145回答 2
2回答

犯罪嫌疑人X

错误是因为findTarget是类函数。所以,你有这个:test ob = new test();String testing = findTarget(target1, source1);...应该改为从静态上下文调用函数://test ob = new test();&nbsp; not needed, the function is staticint 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;&nbsp; // move and initialize testing herewhile ((target1 = br.readLine()) != null) //as long the condition is not null it will keep printing.{&nbsp; &nbsp; //System.out.println(target1);&nbsp; &nbsp; testing += test.findTarget(target1, source1);&nbsp; &nbsp; //target1 = br.readLine();}System.out.println("answer is: "+testing);

冉冉说

我终于能够解决我的问题。但扩展功能。我想将 add 增加 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 {&nbsp; &nbsp; public static int findTarget(String target, String source) {&nbsp; &nbsp; &nbsp; &nbsp; int target_len = target.length();&nbsp; &nbsp; &nbsp; &nbsp; int source_len = source.length();&nbsp; &nbsp; &nbsp; &nbsp; int add = 0;&nbsp; &nbsp; &nbsp; &nbsp; // this function checks the character whether it is present.&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < source_len; ++i) // i is a varialbe used to count upto source_len.&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int j = 0; // take another variable to count loops&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (add == 0)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (j >= target_len) // count upto target length&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (target.charAt(j) != source.charAt(i + j))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++j;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (j == target_len)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add++; // this will return 1: true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return add;&nbsp; &nbsp; &nbsp; &nbsp; //System.out.println(""+add);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String... args) {&nbsp; &nbsp; //String target = "for";&nbsp; &nbsp; // function 1&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // read the file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter your review: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String source1 = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //String source1 = "Searching for a string within a string the hard way.";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File file = new File("write.txt"); //establising a file object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedReader br = new BufferedReader(new FileReader(file)); //reading the files from the file object "file"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String target1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((target1 = br.readLine()) != null) //as long the condition is not null it will keep printing.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //System.out.println(target1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int testing = test.findTarget(target1, source1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("answer is: "+testing);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //target1 = br.readLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; br.close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; catch (IOException e)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("file error!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答