Scanner报错,请高手看看!
import java.util.*;
public class GetNumber {
Scanner sr = new Scanner( "p ");
}
错误信息:构造函数 Scanner(String)未定义,请问是什么原因啊?
------解决方案--------------------你不应该把这句写在类定义的里面 Scanner sr = new Scanner( "p ");
写在构造函数的里面或者某个函数的里面,或者外面加static{}
比如
public GetNumber()
{
Scanner sr = new Scanner( "p ");
}
------解决方案-------------------- 应该这样定义吧
Scanner sr = new Scanner( "System.in ");
然后可以这样读如数据:
String str = sr.next();
------解决方案--------------------public static void main(String[] args) {
// TODO Auto-generated method stub
String input = "1 fish 2 fish red fish blue fish ";
Scanner s = new Scanner(input);
s.findInLine( "(\\d+) fish (\\d+) fish (\\w+) fish (\\w+) ");
MatchResult result = s.match();
for (int i=1; i <=result.groupCount(); i++)
System.out.println(result.group(i));
s.close();
}
我这里没有问题啊,lz检查下