【问题标题】:Can't affect values to a simple Double[] table which is always null不能影响始终为空的简单 Double[] 表的值
【发布时间】:2013-12-16 10:43:01
【问题描述】:

我有一个包含 NMEA 框架的文本文件。我检索 $GPGGA 和 $GPRMC 帧的纬度和经度。这部分还可以。

现在,我想将纬度和经度转换为十进制度。当我尝试将值影响到Double[]coordinatestoconvert 时,就会出现问题。这个总是空的。

好像这个错误真的很白痴,但是我今天早上都在为这种愚蠢而转身......

有人可以帮帮我吗?

这是我正在使用的方法:

public String readText(String filepath) throws Exception
{
    String text="";
    try 
    {
        InputStream inputs=new FileInputStream(filepath);
        InputStreamReader inputsreader=new InputStreamReader(inputs);

        BufferedReader buffer=new BufferedReader(inputsreader);
        String line;
        while((line=buffer.readLine())!=null)
        {
            /* Server send to Client the full line. Then Client will select
             * which data will be retrieve */

            String[]splitedline=line.split(",");
            Double[]decimalcoordinates=retrieveCoordinates(splitedline);

            messagearea.append(decimalcoordinates[0].toString()+","+decimalcoordinates[1].toString());
            tcpserver.sendMessage(decimalcoordinates[0].toString()+","+decimalcoordinates[1].toString());

        }
        buffer.close();
    } 
    catch(FileNotFoundException e) 
    {
        System.out.println(e);
    }   
    return text;
}

public Double[] retrieveCoordinates(String[] splitedline)
{
    Double[]coordinates=null;


    if((splitedline[0]=="$GPGGA") || (splitedline[0]=="$GPRMC"))
    {
        Double[]coordinatestoconvert=null;
        // coordinatestoconvert is always null here
        coordinatestoconvert[0]=Double.parseDouble(splitedline[3]);
        coordinatestoconvert[1]=Double.parseDouble(splitedline[5]);
        coordinates=convertNmeaToDecimal(coordinatestoconvert);
    }
    return coordinates;
}

public Double[] convertNmeaToDecimal(Double[] coordinatestoconvert)
{
    Double[]coordinatesconverted=null;
    for(int i=0;i<2;i++)
    {
        Double degrees=coordinatestoconvert[i]/100;
        Double time=coordinatestoconvert[i]-degrees;

        coordinatesconverted[i]=degrees+time/60;
    }
    return coordinatesconverted;
}

【问题讨论】:

  • 使用.equals() 方法检查字符串。 不要使用==

标签: java gps double nmea


【解决方案1】:
Double[]coordinatestoconvert=null;

这行必须是:

Double[] coordinatestoconvert=new Double[coordinatestoconvert.length];

坐标转换也有同样的问题。

您还应该阅读标准的 java 样式和编码约定,因为它会使您的代码更容易被其他人阅读。

您还使用 == 而不是 .equals 进行字符串比较,这是无效的。

并且尽可能使用 double 而不是 Double 获得更好的性能(如果这对这个程序很重要的话)。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-28
  • 2010-11-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多