亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我想讀取一個文件并檢查一個單詞是否存在于文件中。如果該詞存在,我的一種方法將返回 +1

我想讀取一個文件并檢查一個單詞是否存在于文件中。如果該詞存在,我的一種方法將返回 +1

慕妹3146593 2022-07-27 21:42:50
這是我的代碼。我想讀取一個名為“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 fileFile file = new File("write.txt"); //establising a file objectBufferedReader 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 2test ob = new test();String testing = findTarget(target1, source1);// end    //System.out.println(findTarget(target, source));System.out.println("the answer is: "+testing);}}
查看完整描述

2 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

錯誤是因為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);


查看完整回答
反對 回復 2022-07-27
?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

我終于能夠解決我的問題。但擴展功能。我想將 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 {

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

        }

    }

}


查看完整回答
反對 回復 2022-07-27
  • 2 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號