我需要找到网站的字数。 但每次我运行它时,我都会得到一个错误,说它找不到符号
... = line.split("\\s+");
我尝试将其更改为line.split(“”);
但它仍然无法识别符号split
我是不是丢了个包裹? 还是我用错了?
import java.util.Scanner;
import java.net.MalformedURLException;
import java.io.IOException;
import java.net.URL;
import java.lang.String;
public class CountWords{
public static void main(String[] args) throws Exception{
String URL = "http://websitetips.com/articles/copy/lorem/ipsum.txt";
URL url;
try{
url = new URL(URL);
int wCount = 0;
int wsCount = 0;
Scanner input = new Scanner(url.openStream());
while(input.hasNext()){
String[] line = line.split("\\s+");
wCount += line.length;
wsCount += wCount -1;
}
System.out.println(wCount);
}
catch(MalformedURLException ex){
System.out.println("Invalid URL");
}
catch(IOException ex){
System.out.println("IO Errors");
}
}
}
您缺少行
的源定义。
尝试调整代码,以便可以使用words
:
while(input.hasNext()){
String line = input.next();
String[] words = line.split("\\s+");
wCount += words.length;
wsCount += wCount -1;
}