gpt4 book ai didi

java - 数组越界异常:0

转载 作者:太空宇宙 更新时间:2023-11-04 14:56:50 24 4
gpt4 key购买 nike

我有一个通过语音控制 LED 开关的代码。在下面的处理1.2代码中,发生了数组越界异常错误,请帮助我解决这个问题错误位于初始化函数 2 行...

String words;  
String s;
String str1= "on";
String str2= "off";

void setup () {
initialize () ;
}

void draw () {
listen () ;
}

void respond (String input) {

if(input.length()>0) {
//user speaks any commands
voce.SpeechInterface.setRecognizerEnabled(false) ; //stop
//Listening, decode and send command to robot
String [ ] words = split (input," ");
tts.speak(input) ; //Play the spoken words

if(words[0].equals(str1)==true) {
robot.write ("A") ;
}

if(words[0].equals(str2)==true) {
robot.write ("B") ;
}

voce.SpeechInterface.setRecognizerEnabled(true);
}
}

/*void mousePressed () {

}
*/
//Speech Function:
//import the libraries
import guru.ttslib.*;
import processing.serial.*;

//give our instances names
Serial robot;
TTS tts;
public void initialize() {

voce.SpeechInterface.init("libraries/voce-0.9.1/lib",true,true,"libraries/voce- 0.9.1/lib/gram","digits");
println(Serial.list());
robot = new Serial(this, Serial.list()[0],9600) ; //start serial port and also tts
tts = new TTS() ;
//the following settings control the voice sound
tts.setPitch ( 90 ) ;
tts.setPitchRange ( 90 ) ;
}

void listen () {

if(voce.SpeechInterface.getRecognizerQueueSize()>0 ) {
//if voce recognizes anything being said
s = voce.SpeechInterface.popRecognizedString();
//assign the string that computer heard to the variable s
println("you said: " + s) ;
//print what was heard to the debug window.
respond (s);
}
}

最佳答案

正如评论中所述,我认为您的 split 命令没有返回任何结果。当您尝试访问 words 数组的第 #1 项(位置 0)时,会引发错误。

最好先检查数组中是否有项目:

String [] words = split(input," "); 
if(words.length > 0 && words[0].equals(str1) == true) {
robot.write ("A") ;
}

关于java - 数组越界异常:0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23111368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com