gpt4 book ai didi

java - 当我尝试运行此代码时,它会导致 "Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException : 0"this exception

转载 作者:行者123 更新时间:2023-12-01 23:27:07 26 4
gpt4 key购买 nike

import javax.speech.*;
import java.util.*;// creates a default Systhesize
import javax.speech.synthesis.*; //code for text-speech engine

public class SpeechUtils
{
String speaktext;
public void dospeak(String speak,String voicename)
{
speaktext=speak;
String voiceName =voicename;
try
{
SynthesizerModeDesc desc = new
SynthesizerModeDesc(null,"general",Locale.US,null,null);
Synthesizer synthesizer = Central.createSynthesizer(desc);
synthesizer.allocate();
synthesizer.resume();
desc = (SynthesizerModeDesc)
synthesizer.getEngineModeDesc();
Voice[] voices = desc.getVoices();
Voice voice = null;
for (int i = 0; i < voices.length; i++)
{
if (voices[i].getName().equals(voiceName))
{
voice = voices[i];
break;
}
}
synthesizer.getSynthesizerProperties().setVoice(voice);
synthesizer.speakPlainText(speaktext, null);

synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
synthesizer.deallocate();
}catch (Exception e)
{
String message = " missing speech.properties in " + System.getProperty("user.home") + "\n";
System.out.println(""+e);
System.out.println(message);}
}

public static void main(String[] args)
{
SpeechUtils obj=new SpeechUtils();
obj.dospeak(args[0],"life is beautiful");//getting an exception here....
}

}

如何解决这个异常?我收到类似

的错误
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at SpeechUtils.main(SpeechUtils.java:44)".

这是一个Java程序或文本到语音转换。我导入了所有必需的库。但无法单独修复此异常

最佳答案

您需要在运行程序时提供运行时参数。就像在您的代码中一样,您访问第一个参数:

obj.dospeak(args[0],"life is beautiful");//getting an exception here....

如果您不提供参数,则 args 数组的索引 0 处将没有元素,因此使用 args[0] 访问它会抛出 ArrayIndexOutOfBoundsException。您需要像这样运行代码:

java SpeechUtils argument 

将参数替换为您想要输入到程序中的实际值

关于java - 当我尝试运行此代码时,它会导致 "Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException : 0"this exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19827719/

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