【问题标题】:Arduino to Processing, Strings ArrayIndexOutOfBoundsException: 18Arduino 到处理,字符串 ArrayIndexOutOfBoundsException:18
【发布时间】:2014-05-02 23:14:57
【问题描述】:

我正在开发一个程序,该程序将消息从 Arduino 发送到处理,将它们作为十进制数字从 arduino 打印到串行端口,然后在处理中将它们提取到字符串数组中。我遇到的问题是:“ArrayIndexOutOfBounds:18”,它突出显示 dlay = Integer.parseInt(B[18]) 的行,我认为该数组不够大,无法存储 17 岁以后的任何内容,而且我我可能犯了一个非常愚蠢的错误,但我不知道如何扩展它!

有什么建议吗?

提前非常感谢!

import processing.serial.*; //import the Serial library

int end = 10;
String serial;
Serial port;
int c;
int d;
int e;
int f;
int g;
int a;
int b;
int C;
int p1t;
int p2t;
int p3t;
int p4t;
int p5t;
int p6t;
int p7t;
int p8t;
int pan;
int reverb;
int dlay;
int distort;
//ellipse parameters
int noteOn = 0;
int col1 = 0;
int col2 = 0;
int col3 = 0;
String[] A;
String[] B;
void setup() {
    size(600,600);
    frameRate(30);
    port = new Serial(this, Serial.list()[7], 115200);
    port.clear();
    serial = port.readStringUntil(end);
    serial = null; // initially, the string will be null (empty)
}

void draw() {

while (port.available() > 0) { 
serial = port.readStringUntil(end);

}
if (serial != null) {  //if the string is not empty, print the following

  A = split(serial, ','); 
  B = trim(A);
  c = Integer.parseInt(B[1]);
  if (c == 1){
  col1 = 255;
  col2 = 0;
  col3 = 0;
  }
  d = Integer.parseInt(B[2]);
  if (d == 1){
  col1 = 0;
  col2 = 0;
  col3 = 255;
  }
  e = Integer.parseInt(B[3]);
  if (e == 1){
  col1 = 0;
  col2 = 255;
  col3 = 0;
  }
  f = Integer.parseInt(B[4]);
  if (f == 1){
  col1 = 255;
  col2 = 0;
  col3 = 255;
  }
  g = Integer.parseInt(B[5]);
  if (g == 1){
  col1 = 255;
  col2 = 255;
  col3 = 0;
  }
  a = Integer.parseInt(B[6]);
  if (a == 1){
  col1 = 0;
  col2 = 255;
  col3 = 255;
  }
  b = Integer.parseInt(B[7]);
  if (b == 1){
  col1 = 50;
  col2 = 250;
  col3 = 130;
  }
  C = Integer.parseInt(B[8]);
  if (C == 1){
  col1 = 200;
  col2 = 90;
  col3 = 75;
  }
if(c == 1 || d == 1 || e == 1 || f == 1 || g == 1 || a == 1 || b == 1 || C == 1){
  noteOn = 1;
}
else {
  noteOn = 0;
}
p1t = Integer.parseInt(B[9]);
p2t = Integer.parseInt(B[10]);
p3t = Integer.parseInt(B[11]);
p4t = Integer.parseInt(B[12]);
p5t = Integer.parseInt(B[13]);
p6t = Integer.parseInt(B[14]);
p7t = Integer.parseInt(B[15]);
p8t = Integer.parseInt(B[16]);
pan = Integer.parseInt(B[17]);
dlay = Integer.parseInt(B[18]);
//reverb = Integer.parseInt(B[19]);
//distort = Integer.parseInt(B[18]);
} 
fill(0, 0,0, 255);
rect(0,0, 600,600);
fill(col1,col2, col3); 
ellipse(300+pan, 300, (noteOn*p8t), (noteOn*p8t));


}

【问题讨论】:

    标签: arrays string arduino processing


    【解决方案1】:

    您是否知道 Java 中的数组(以及 Processing 中的数组)从 0 而不是 1 开始索引?也就是说,一个名为exampleArray 的数组的第一个元素是通过写exampleArray[0] 来访问的,第二个是通过exampleArray[1] 来访问的,等等。所以如果你的数组中有18 个元素,写B[18] 实际上是试图得到一个不存在的第 19 个元素。

    附注:拥有这么多变量被认为是不好的做法,尤其是对于这么短的名称——我不知道abc 等通过查看它们的名称在做什么,后面的代码也没有解释。一些字母有小写和大写两种版本,这使情况更加复杂!您现在知道它们是什么,但是如果您在几个月后回到此代码,那么您将不得不弄清楚。

    【讨论】:

    • 嗨,凯文,感谢您的回答!变量名是音符,所以小写 c 是音符 c,d e f g a b,大写 C 是音符 C 一个八度以上。我怎样才能避免所有这些变量呢?既然它们都必须转换并用于控制不同的东西?而且我知道零索引,但是即使我将 50 放在方括号中,我仍然收到错误 java.lang.ArrayIndexOutOfBounds: 18,当我添加第 18 个 integer.ParseInt 行时,你知道字符串是否有一定的长度还是什么?实在想不通!
    • 如果你在调用B = trim(A);之后立即插入println(B);,你会看到什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 2011-11-16
    相关资源
    最近更新 更多