Java 在现有类中实现类
我正试图将我的语音识别器放入计算器应用程序中,以便盲人可以使用它。代码如下:Java 在现有类中实现类,java,Java,我正试图将我的语音识别器放入计算器应用程序中,以便盲人可以使用它。代码如下: public class HelloWorld { public static Recognizer recognizer; public static void init(String[] args) { try { URL url; if (args.length > 0) { url = ne
public class HelloWorld {
public static Recognizer recognizer;
public static void init(String[] args) {
try {
URL url;
if (args.length > 0) {
url = new File(args[0]).toURI().toURL();
} else {
url = HelloWorld.class.getResource("helloworld.config.xml");
}
System.out.println("Loading...");
ConfigurationManager cm = new ConfigurationManager(url);
recognizer = (Recognizer) cm.lookup("recognizer");
Microphone microphone = (Microphone) cm.lookup("microphone");
/* allocate the resource necessary for the recognizer */
recognizer.allocate();
/* the microphone will keep recording until the program exits */
if (microphone.startRecording()) {
System.out.println
("Say: (Good morning | Hello) " +
"( Bhiksha | Evandro | Paul | Philip | Rita | Will )");
System.out.println
("Start speaking. Press Ctrl-C to quit.\n");
/*
* This method will return when the end of speech
* is reached. Note that the endpointer will determine
* the end of speech.
*/
}
} catch (IOException e) {
System.err.println("Problem when loading HelloWorld: " + e);
e.printStackTrace();
} catch (PropertyException e) {
System.err.println("Problem configuring HelloWorld: " + e);
e.printStackTrace();
} catch (InstantiationException e) {
System.err.println("Problem creating HelloWorld: " + e);
e.printStackTrace();
}
}
public static int convertToInt(String num){
int tmp = 0;
switch(num){
case "one": tmp = 1; break;
case "two": tmp = 2; break;
case "three": tmp = 3; break;
case "four": tmp = 4; break;
case "five": tmp = 5; break;
case "six": tmp = 6; break;
case "seven": tmp = 7; break;
case "eight": tmp = 8; break;
case "nine": tmp = 9; break;
default: tmp = -1;
}
System.out.print("The number is: " + tmp);
return tmp;
}
public static int check(){
Result result = recognizer.recognize();
String resultText = "";
if (result != null) {
resultText = result.getBestFinalResultNoFiller();
System.out.println("-"+resultText+"-");
System.out.println("You said: " + resultText + "\n");
} else {
System.out.println("I can't hear what you said.\n");
}
return convertToInt(resultText);
}
}
下面是应用程序的主循环:
public static void main(String[] args) {
HelloWorld.init(args);
SwingUtilities.invokeLater( new Runnable() {
public void run() {
new Main();
HelloWorld.check();
}
});
}
我似乎无法让它们同时运行,有人能帮我解决这个问题吗?您需要两个不同的线程
Main
在Swing线程中运行,而HelloWorld
在另一个线程中运行,例如主线程
我还不清楚他们将如何沟通。无论如何,这就是如何在两个不同的线程中启动它们:
public static void main(String[] args) {
HelloWorld.init(args);
SwingUtilities.invokeLater( new Runnable() {
public void run() {
new Main();
}
});
HelloWorld.check();
}
但是,请注意,此代码不会解决您的问题。您可以阅读有关Swing线程的内容。您的主类定义在哪里?该
Main
属于哪个类?HelloWorld.main在哪里?它做什么?什么是Main
;应该运行计算器吗?“让它们同时运行”是什么意思?计算器代码是什么样子的?更重要的是:识别器应该如何与计算器交互?@tamasrev是的,它在计算器代码中。我把这两门课分开。它只会通过提供信息来做出反应,然而,它们不是并排运行的,一个总是在等待另一个。