第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

慕容3067478 2022-08-03 15:32:29
這是我的代碼。我想讀取一個名為“write.txt”的文件,然后一旦它讀取。將其與一個單詞進(jìn)行比較,在這里我使用“目標(biāo)變量(字符串類型)”一旦在名為findTarget的方法內(nèi)完成比較,它將在條件為真后返回1。我嘗試調(diào)用該方法,但我不斷收到錯誤。test.java:88: 錯誤: 找不到符號 字符串測試 = findTarget(target1, 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 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 回答

?
溫溫醬

TA貢獻(xiàn)1752條經(jīng)驗 獲得超4個贊

錯誤是因為是類函數(shù)。findTarget


所以,你有這個:


test ob = new test();


String testing = findTarget(target1, source1);

...應(yīng)更改為從靜態(tài)上下文調(diào)用函數(shù):


//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.

我沒有您的文件內(nèi)容進(jìn)行試運行,但這至少應(yīng)該有助于克服錯誤。


===== 更新:


你離你很近!


在main中,更改循環(huán)中的代碼,使其如下所示:


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


查看完整回答
反對 回復(fù) 2022-08-03
?
ibeautiful

TA貢獻(xiàn)1993條經(jīng)驗 獲得超6個贊

我終于能夠解決我的問題。但擴展了功能。我想將加法遞增 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!");

        }

    }

}



查看完整回答
反對 回復(fù) 2022-08-03
  • 2 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號