繁体   English   中英

数组超出范围异常:0

[英]array out of bound exception:0

我有控制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") ;    
}

暂无
暂无

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

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