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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何查找某個(gè)單詞在文本文件的哪一行,如果該單詞存在于多行中,則保存行號(hào)?

如何查找某個(gè)單詞在文本文件的哪一行,如果該單詞存在于多行中,則保存行號(hào)?

慕村225694 2023-07-19 15:50:03
我可以找到該單詞的出現(xiàn)位置,但無(wú)法找到該單詞所在的行號(hào),以及有什么方法可以像數(shù)組列表一樣保存行號(hào)?  File f1=new File("input.txt")  String[] words=null;  //Intialize the word Array  FileReader fr = new FileReader(f1);  //Creation of File Reader object  BufferedReader br = new BufferedReader(fr);   String s;       String input="Java";   // Input word to be searched  int count=0;   //Intialize the word to zero  while((s=br.readLine())!=null)   //Reading Content from the file  {     words=s.split(" ");  //Split the word using space      for (String word : words)       {             if (word.equals(input))   //Search for the given word             {               count++;    //If Present increase the count by one             }      }  }  if(count!=0)  //Check for count not equal to zero  {     System.out.println("The given word is present for "+count+ " Times in the file");  }  else  {     System.out.println("The given word is not present in the file");  }     fr.close();   }   }
查看完整描述

3 回答

?
千萬(wàn)里不及你

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊

有一個(gè)計(jì)數(shù)器并對(duì)每一行進(jìn)行計(jì)數(shù)。


    long count = 0;

    long lineNumberCounter = 0;

    List<Long> lineNumbers = new ArrayList<>();

    try (BufferedReader b = new BufferedReader(new java.io.FileReader(new File(fileName)))) {


        String readLine = "";


        System.out.println("Reading file using Buffered Reader");


        while ((readLine = b.readLine()) != null) {

            // Here is line number counter

            lineNumberCounter++;

            String[] words = readLine.split(" "); // Split the word using space

            System.out.println(Arrays.toString(words));

            for (String word : words) {

                // Search for the given word

                if (word.trim().equals(input)) {

                    count++; // If Present increase the count by one

                    System.out.println("Word " + input + " found in line " + lineNumberCounter);

                    lineNumbers.add(lineNumberCounter);

                }

            }

        }

    }


    // Check for count not equal to zero

    if (count != 0) {

        System.out.println("The given word is present for " + count + " Times in the file");

    } else {

        System.out.println("The given word is not present in the file");

    }


查看完整回答
反對(duì) 回復(fù) 2023-07-19
?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊

我認(rèn)為這會(huì)有所幫助。

您所要做的就是跟蹤行號(hào),然后保存該單詞可用的行


File f1=new File("input.txt")

String[] words=null;  //Intialize the word Array

FileReader fr = new FileReader(f1);  //Creation of File Reader object

BufferedReader br = new BufferedReader(fr); 

String s;     

String input="Java";   // Input word to be searched

int count=0;   //Intialize the word to zero


// for keeping track of the line numbers

int lineNumber= 0; 


//arraylist to save the numbers

List<int> lineNumberList = new ArrayList<>();


while((s=br.readLine())!=null)   //Reading Content from the file

{

  // increase the line number as we move on to the next line

  lineNumber++;


 words=s.split(" ");  //Split the word using space


  // this is required so that same line number won't be repeated on the arraylist

  boolean flag = true;

  for (String word : words) 

  {


         if (word.equals(input))   //Search for the given word

         {

           count++;    //If Present increase the count by one

           if(flag){

               lineNumberList.add(lineNumber);

               flag=false;

            }

         }

  }

 }

if(count!=0)  //Check for count not equal to zero

 {

 System.out.println("The given word is present for "+count+ " Times in the file");

 }

 else

  {

 System.out.println("The given word is not present in the file");

 }


 fr.close();

 }

}


查看完整回答
反對(duì) 回復(fù) 2023-07-19
?
尚方寶劍之說(shuō)

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

嘗試使用LineNumberReader而不是BufferedReader. 它支持 BufferedReader 和 LineNumber。


Javadoc 了解更多信息 - https://docs.oracle.com/javase/8/docs/api/java/io/LineNumberReader.html。


例子 -


LineNumberReader lineNumberReader = 

    new LineNumberReader(new FileReader("c:\\data\\input.txt"));


int data = lineNumberReader.read();

while(data != -1){

    char dataChar = (char) data;

    data = lineNumberReader.read();

    // your word processing happens here

    int lineNumber = lineNumberReader.getLineNumber();

}

lineNumberReader.close();

http://tutorials.jenkov.com/java-io/linenumberreader.html


查看完整回答
反對(duì) 回復(fù) 2023-07-19
  • 3 回答
  • 0 關(guān)注
  • 185 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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