Android 将字符串转换为int,生成地理点

Android 将字符串转换为int,生成地理点,android,string,int,geo,Android,String,Int,Geo,我有一个文件,每行都有gps坐标: 9.34669876098644,48.2405319213867 9.36384963989269,48.2296714782715 9.3639497756958,48.2259712219238 9.36956977844238,48.2260589599609 9.36404991149908,48.2222709655763 9.36975955963135,48.2186508178712 现在,我想解析文件并进行地理点。首先我打开文件并读取该行

我有一个文件,每行都有gps坐标:

9.34669876098644,48.2405319213867
9.36384963989269,48.2296714782715
9.3639497756958,48.2259712219238
9.36956977844238,48.2260589599609
9.36404991149908,48.2222709655763
9.36975955963135,48.2186508178712
现在,我想解析文件并进行地理点。首先我打开文件并读取该行,然后分割该行并希望将字符串设为int。但是,在Log.d之后,我得到了一个NullPointerException(“4:,point_t[0]+”-“+point_t[1])

@呆板的

Log.d("1: ", "Start ArrayList");
ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
try{
    InputStream koord = getAssets().open("bib.txt");
    if (koord != null) {
        InputStreamReader input = new InputStreamReader(koord);
        BufferedReader buffreader = new BufferedReader(input);
        Log.d("2: ", "Read File");
        String line;
        while (( line = buffreader.readLine()) != null) {
            Log.d("3: ", line);
            String[] point_t = line.split(",");
            Log.d("4:",point_t[0] + " - " + point_t[1]);
            double x = Double.parseDouble(point_t[0]);
            double y = Double.parseDouble(point_t[1]);
            Log.d("Geopoint:",x + " - " + y);
            points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
            Log.d("5:","saved");
        }
        koord.close();
        polygon = new Polygon(points);
    }
}catch (Exception e) {
    Log.e("APP","Failed", e);
    }        
    mapView.getOverlays().clear();
    mapView.getOverlays().add(polygon);
    mapView.invalidate();
}
解决方案:

现在它可以工作了,我使用eclipse new中的coords创建了该文件,并立即使用它:

ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
try{
    InputStream koord = getAssets().open("biberach");
    if (koord != null) {
        InputStreamReader input = new InputStreamReader(koord);
        BufferedReader buffreader = new BufferedReader(input);
        String line;
        while (( line = buffreader.readLine()) != null) {
            String[] point_t = line.split(",");
                double y = Double.parseDouble(point_t[0]);
                double x = Double.parseDouble(point_t[1]);
                points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
        }
        koord.close();
        polygon = new Polygon(points);
    }
}catch (Exception e) {
    Log.e("APP","Failed", e);
} 
ArrayList points=new ArrayList();
试一试{
InputStream koord=getAssets().open(“biberach”);
if(koord!=null){
InputStreamReader输入=新的InputStreamReader(koord);
BufferedReader buffreader=新的BufferedReader(输入);
弦线;
而((line=buffreader.readLine())!=null){
String[]point\u t=line.split(“,”);
double y=double.parseDouble(点[0]);
double x=double.parseDouble(点[1]);
添加(新的地质点((int)(x*1e6),(int)(y*1e6));
}
koord.close();
多边形=新多边形(点);
}
}捕获(例外e){
Log.e(“应用程序”,“失败”,e);
} 

文件出现错误

,因为您正在捕获并忽略该块中的任何异常,请尝试在空捕获区域中注销该异常,您将看到收到一个
NumberFormatException

Exception in thread "main" java.lang.NumberFormatException: For input string: "9.34669876098644"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:481)
    at java.lang.Integer.parseInt(Integer.java:514)
    at TestParseInt.main(TestParseInt.java:4)
您不能对一个双倍数执行
parseInt
(即使它确实有效,您也只能得到整数部分,这不是您想要的)。尝试改用
Double.parseDouble()

换句话说

        double x = Double.parseDouble(point_t[0].trim());
        double y = Double.parseDouble(point_t[1].trim());
而不是这个

}catch (Exception e) {
    // TODO: handle exception
}
试用

}catch (Exception e) {
    Log.e("APP","Failed", e);
}
更新1: 也许你已经删除了空格,所以使用
split(“\\s*,\\s*)
,然后在字符串上使用
.trim()
。Double.parseDouble()应该允许空白

更新2: 在您的
locale
中,
decimal
是有效的分隔符还是
逗号
?如果是,您可能需要使用
DecimalFormat
解析值并强制使用
US
locale

我从你的个人资料中看到你在德国,但你解析的数字不是德语。所以,试试这个

DecimalFormat parser = (DecimalFormat)DecimalFormat.getInstance(Locale.US);
double x = (Double)parser.parse("48.2405319213867");

这迫使解析器使用US语言环境来解析数字(即,.is decimal)。

您做错了。在文本文件中,字符串首先以空格为底,然后以为底;“作为:

    String line="";
    while (( line = buffreader.readLine()) != null) {
        Log.d("3: ", line);
        line +=line;
    }
    String[] point_arr = line.split(" ");
    for (int i = 0; i <= point_arr.Length - 1; i++)
    {
        String[] point_t = point_arr[i].split(",");
        Log.d("4:",point_t[0] + " - " + point_t[1]);
        int x = Integer.parseInt(point_t[0]);
        int y = Integer.parseInt(point_t[1]);
        Log.d("Geopoint:",x + " - " + y);
        points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
        Log.d("5:","saved");
    }
字符串行=”;
而((line=buffreader.readLine())!=null){
Log.d(“3:,行);
行+=行;
}
String[]point\u arr=line.split(“”);

对于(int i=0;i首先,你必须得到所有的行字符串[]。然后,正如你所看到的,你必须按“”(空格)分割得到两个坐标,然后这些字符串再按“,”分割得到logtitude和latitute。因此,一行得到两个坐标,而不是一个

还有这个部分

        int x = Integer.parseInt(point_t[0]);
        int y = Integer.parseInt(point_t[1]);
        Log.d("Geopoint:",x + " - " + y);
        points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
不好,应该是:

        double x = Double.parseDouble(point_t[0]);
        double y = Double.parseDouble(point_t[1]);
        Log.d("Geopoint:",x + " - " + y);
        points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
因为,当你有20.123456号时,你得到20000000,而我的方法得到20123456

另外,在捕获之前,再添加一个捕获

catch(NumberFormatException e) {
    //this occurs when can't convert String to specified number format, so it means, that you give bad number to parse
}

它不能回答您的确切问题,但它可以帮助您完成您的工作

这是OverlyBundle中的第42行?是否确定“points”变量已初始化?它看起来无法将给定值解析为double(或任何其他数字),可能是因为“.char”,所以将其更改为“,”,by.replace(“,”)。例如
String value=“56456.4564“.replace(“.”,“,”);
,因此请尝试
double x=double.parseDouble(点[0]。replace(“.”,“,”));
和y+1相同,这是一个很好的点……即使我认为在打印D/point:(231)之后,流到哪里去了: 9.34669876098644 - 48.2405319213867@DheereshSingh:你能看到我的答案吗?bez我想他吐错了字符串是的,Imran,但我想我们在这里对buffreader得到的每一行执行操作……并且流也将记录到Log.d(“4:,point_t[0]+”-“+point_t[1]),而不是记录到Log.d(“geoppoint:,x+”-“+y);根据日志cat,所以问题应该在这些行之间…..双值看起来不错,我在设备上解析了它们(只是为了确保),所以我不知所措。您可以尝试更改拆分和解析语句以删除空格,看看这是否有帮助。(我更新了答案以包含此内容)在您的区域设置中,十进制是“十进制”的有效数字占位符还是逗号。例如,典型的双精度字符是“9.5”还是“9,5”,如果是后者,请查看DecimalFormat(我更新了答案以反映这一点)
        double x = Double.parseDouble(point_t[0]);
        double y = Double.parseDouble(point_t[1]);
        Log.d("Geopoint:",x + " - " + y);
        points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
catch(NumberFormatException e) {
    //this occurs when can't convert String to specified number format, so it means, that you give bad number to parse
}