数组超出绑定异常:0

时间:2014-04-16 13:48:03

标签: java arrays processing speech-recognition

我有一个通过语音控制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);         
   }          
}         

1 个答案:

答案 0 :(得分:0)

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

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

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