(Java + Android)解析字符串浮动奇怪的错误

时间:2015-03-07 13:23:37

标签: java android string parsing

这可能听起来像一个微不足道的问题,但我很难找到它。基本上我是从我的Android发送一个字符串到我的电脑。所有连接都正常,字符串成功传输。这是Android代码(将字符串发送到计算机):

  try
  {
    println(scSocket + "");
    if (scSocket!=null) 
    {
    SendReceiveBytes sendReceiveBT = new SendReceiveBytes(scSocket);
    String red = rotZ + " \n";
    byte[] myByte = stringToBytesUTFCustom(red);
    sendReceiveBT.write(myByte);
    }
  }
  catch(Exception e)
  {
    println(e);
  }

rotZ是我要发送的地方,它是一个" float"值。我需要把" \ n"在消息的末尾,以便它将被识别为PC上的完整消息。到现在为止还挺好。现在我想在我的电脑上阅读这个,这是通过以下方式实现的:

  //BlueTooth
  String lineRead = "";
  try
  {
    lineRead = new String(sampleSPPServer.readFromDevice());
    if(lineRead != null && !lineRead.isEmpty())
    {
      String lineTransf = lineRead.replace("\n", "").replace("\r", "").replace(" ", "").replace("\"", "").trim();
      println("LineTransf: " + lineTransf);
      rotZ += 0.01*(Float.parseFloat(lineTransf));
      println("Zrotation: " + rotZ); //Never gets here, throws and error before...
    }
    else
      rotZ += 0;
  }
  catch(Exception e)
  {
    println("Exception: " + e);
  }

这给了我错误:

NumberFormatException: invalid float value: "1.1400002"

在我的代码中,您可以看到我检查null,空等等。所以这不是问题所在。我已经尝试过了:

  NumberFormat nf = NumberFormat.getInstance(Locale.US);
  rotZ += 0.01*(nf.parse(lineTransf).floatValue());

得到相同的结果...在stackoverflow中有一个类似的问题: Here

还有一件奇怪的事情,如果我尝试代码:

  for(int i = 0; i < lineTransf.length(); i++)
    println(lineTransf.substring(i,1));

我知道字符串的长度是19,但它只打印前两个并给出消息:

Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

更奇怪的是,当我在数字&#34; 1.1400002&#34;上执行ctrl-c,ctrl-v时出现在控制台中,它只粘贴&#34; 1&#34;这里堆栈溢出。 我知道这个数字是正确的,但转换的地方不是。我认为这是因为字符串是作为字节发送并作为字符串读取,但我该如何解决这个问题呢?在此先感谢!!

2 个答案:

答案 0 :(得分:0)

没什么奇怪的。这是substring的预期行为。它会引发IndexOutOfBoundsException,如果startIndex为否定,endIndex大于字符串的长度,或startIndex大于endIndex(这是你的for(int i = 0; i < lineTransf.length(); i++) println(lineTransf.charAt(i)); 案件)。对我来说,看起来你想在索引处打印char。试试

{{1}}

答案 1 :(得分:0)

我找到了一个解决方法,但我确实想要一个解释(如果可能的话),因为这太难看了......我把代码更改为:

//BlueTooth
  String lineRead = "";
  try
  {
    lineRead = new String(sampleSPPServer.readFromDevice());
    if(lineRead != null && !lineRead.isEmpty())
    {
      String lineTransf = lineRead.replace("\n", "").replace("\r", "").replace(" ", "").replace("\"", "").trim();
      println("LineTransf: " + lineTransf + " " + lineTransf.length());
      String lastTry = "";
      for(int i = 0; i < lineTransf.length(); i++)
      {
        if(lineTransf.charAt(i) != ' ' && lineTransf.charAt(i) != '\u0000')
        {
          println(lineTransf.charAt(i));
          lastTry += lineTransf.charAt(i);
        }
      }
      println("LastTry: " + lastTry);
      rotZ += 0.01*(Float.parseFloat(lastTry));
      println("Zrotation: " + rotZ);
    }
    else
      rotZ += 0;
    //System.out.println("Line Read:" + lineRead);
  }
  catch(Exception e)
  {
    println("Exception: " + e);
  }

我基本上创建一个名为lastTry的新String,然后检查每个蓝牙读取字符是否为空(?)null(?)(因为我正在测试:)

if(lineTransf.charAt(i) != ' ' && lineTransf.charAt(i) != '\u0000')

如果他们通过了这个测试,我就会单独组装#34; lastTry字符串。 似乎蓝牙正在整个字符串的每个字符之间发送一个空字符。我不明白为什么会发生这种情况,并且在读取传入的字符串时实际上会耗费一些时间。如果有人有另一个想法我真的会喜欢另一个答案......