gpt4 book ai didi

java - 如何使用扫描仪捕获txt文件中的字符串?

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:32 27 4
gpt4 key购买 nike

我使用此示例代码对一个文本文件进行一次搜索:

private void buildLink(){

int wordCount = 0, totalcount = 0;
Scanner s = new Scanner(googleNode);
while (s.hasNext()) {
totalcount++;

if (s.next().equals("href")) wordCount++;
}
System.out.println(wordCount+" "+totalcount);
}

但我的问题是 s.hasNext 跳过了搜索中的大部分单词(可能是因为文本文件是一个 html 代码并且几乎没有空格字符)。对于此示例代码,输出计数为:

字数 = 0总数 = 18056

那么,我到底做错了什么,我应该做什么来解决这个问题,因为我想要的是捕获这个 html 代码中的一个链接并传递给一个字符串变量?

我想到的一种方法是将整个 html 代码放在一个字符串中,然后处理搜索,但这对于一名优秀的程序员来说太可笑了..

有人可以帮我吗?提前致谢

最佳答案

if (s.next().contains("href")) wordCount++; } System.out.println  (wordCount+" "+totalcount); } 

我认为这一定行得通

关于java - 如何使用扫描仪捕获txt文件中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17784526/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com