怎样从字符串中提取数字

来源:百度知道 编辑:UC知道 时间:2024/05/14 06:52:12
下面是我写的代码,可是总是有错(java.lang.NullPointerException),谁能帮我看下:
public double[] getNumber(String s){
double[] a=null;
String regex = "\\d+";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
if(m.find()){
String sr = m.group();
int k=0;
a[k]=Double.parseDouble(sr);//此行有误
}
return a;
}
double[] a=null;
a 中的个数开始根本不知道啊

只需要将
double[] a=null;
改成
double[] a = new double[s.length];
就行了..

double[] a=null;
没有为数组申请地址,如下定义一下就好了:
double[] a = new double[1];

1楼。

你先循环一遍把数字的个数求出来,然后再分配一个数组,再循环一遍初始化数组