java 字母搜索_使用正则表达式Java搜索字母

你在这一行上有一个NullPointerException:

String word = sc.next();这意味着您的Scanner sc未初始化。如果您没有找到该文件并且抛出了FileNotFoundException,则会发生这种情况。您无法查看是否因为您在catch块中没有执行任何操作而抛出此类异常。

尝试这个 :

public static void exp(String uttryck){

int counter = 0;

File fil = new File("Walta_corpus1.txt");

Scanner sc = null;

try{

sc = new Scanner(fil);

String word = sc.next();**

Pattern pattern = Pattern.compile(uttryck);

Matcher matcher = pattern.matcher(word);

while(word != null){

if(matcher.find()){

counter++;

System.out.println(word);

}

if(sc.hasNext()){

word=sc.next();

matcher = pattern.matcher(word);

} else

word = null;

}

System.out.println(counter);

} catch(FileNotFoundException foo){

foo.printStackTrace();

}

}我认为拥有空的catch块是一个非常糟糕的主意。尝试至少打印一个打印件,以了解是否有异常。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值