简体   繁体   English

处理字符串到整数的转换错误

[英]Processing String To Int Conversion Error

I have some simple Processing code that reads serial data coming in, writes it to a string and, depending on what is contained within that string, it converts the string to an int or char and executes some specific code. 我有一些简单的处理代码,用于读取传入的串行数据,将其写入字符串,然后根据该字符串中包含的内容将字符串转换为int或char并执行一些特定的代码。

My problem is that when I try to convert the data to an integer it either gives me an error or gives me a value of zero ( int(string) returns zero, Integer.parseInt(string) returns an error ). 我的问题是,当我尝试将数据转换为整数时,它要么给我一个错误,要么给我一个零值(int(string)返回零,Integer.parseInt(string)返回error)。 Below is the relevant portion of my code. 以下是我代码的相关部分。

String serialreadbuffer;
int t = 0;
boolean STimer = false;

// Serial Message Display (From Controller)
if (myPort.available() > 0) 
{  // Check if data is available
  serialreadbuffer = myPort.readStringUntil('\n');         // read it and write to variable
  if (serialreadbuffer.charAt(0) == 'I')
  {
    SerialText.setText("INITIALIZED");
    t = 0;
    STimer = true;
  }
  else if (serialreadbuffer.charAt(0) == 'U')
  {
    SerialText.setText("UPDATED");
    t = 0;
    STimer = true;
  }
  else if (serialreadbuffer.charAt(0) == 'D')
  {
    SerialText.setText("DISABLED");
    t = 0;
    STimer = true;
  }
  else if (serialreadbuffer.charAt(0) == 'E')
  {
    SerialText.setText("SERIAL COMMUNICATION ERROR");
    t = 0;
    STimer = true;
  }
  else
  {
    int numberbuffer = Integer.parseInt(serialreadbuffer) * 60 / 6144;
    serialreadbuffer = str(numberbuffer);
    RPMText.setText(serialreadbuffer);
  }
}

Strangely enough, if I assign a string value directly to serialreadbuffer (ie serialreadbuffer = "6144") it will work, but if I get the same number from my serial port it gives me the following error: 奇怪的是,如果我直接将字符串值分配给serialreadbuffer(即serialreadbuffer =“ 6144”),它将起作用,但是如果我从串行端口获得相同的数字,则会出现以下错误:

NumberFormatException: For input string: "6144"

and it gives me that error for the following line: 它给我以下行的错误:

int numberbuffer = Integer.parseInt(serialreadbuffer) * 60 / 6144;

I was also having a problem with the string.equals() compares (it would always return false, even if the strings were identical), however I got around this by converting the first character to char and using that to compare. 我的string.equals()比较也有问题(即使字符串相同,它也总是返回false),但是我通过将第一个字符转换为char并使用它进行比较来解决此问题。 Not sure if that is related to this issue or not. 不知道这是否与此问题有关。

I have scoured the internet and have found no help with this matter. 我搜寻了互联网,但没有找到任何帮助。 I am not an experienced programmer, so maybe it's something obvious that I'm just not seeing. 我不是一个经验丰富的程序员,所以也许我看不到它。 Your help is greatly appreciated. 非常感谢您的帮助。

trim the String before you parse it insetad of 在解析字符串之前先修剪字符串

int numberbuffer = Integer.parseInt(serialreadbuffer) * 60 / 6144;

do

int numberbuffer = Integer.parseInt(serialreadbuffer.trim()) * 60 / 6144;

you might have unprintable characters eg \\r in it. 您可能有不可打印的字符,例如\\ r。

There could be some space with the value. 该值可能会有一些空间。 Try trimming the string before conversion. 尝试在转换前修剪字符串。

Serialreadbuffer.trim() Serialreadbuffer.trim()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM