我正在使用Fretts api。下面是代码片段。
public static void main(String[] args){
Scanner src=new Scanner(System.in);
String st,classname,filename;
int input=0;
char O_curlybrace,O_roundbrace,C_curlybrace,C_roundbrace;
File f=null;
boolean boo=false;
try
{
System.setProperty("freetts.voices",
com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
Central.registerEngineCentral
("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
Synthesizer synthesizer =
Central.createSynthesizer(new SynthesizerModeDesc(Locale.US));
synthesizer.allocate();
synthesizer.resume();
System.out.println("Welcome to world of programming");
synthesizer.speakPlainText("Welcome to world of programming", null);
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
synthesizer.speakPlainText("Start your java programming now.", null);
System.out.println("Start your java programming now");
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
synthesizer.speakPlainText("Enter your file name", null);
System.out.println("Enter your file name");
st=src.next();
st=
filename=st.substring(0,1).toUpperCase();
classname=filename.concat(st.substring(1));
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
synthesizer.speakPlainText("Creating new java file", null);
System.out.println("Creating new java file");
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
FileWriter fstream = new FileWriter(classname+".txt",true);
BufferedWriter output = new BufferedWriter(fstream);
output.write("import java.io.*;");
output.newLine();
output.append("import java.util.*");
output.newLine();
synthesizer.speakPlainText("File created", null);
System.out.println("File created: ");
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
synthesizer.speakPlainText("Importing basic packages.", null);
System.out.println("Importing basic packages.");
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
Thread.sleep(1500);
synthesizer.speakPlainText("Packages imported.", null);
System.out.println("Packages imported");
Thread.sleep(500);
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
synthesizer.speakPlainText("Enter 1 to create public class", null);
System.out.println("Enter 1 to create public class");
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
input=src.nextInt();
if(input==1)
{
synthesizer.speakPlainText("Creating public class", null);
System.out.println("Creating public class");
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
Thread.sleep(2000);
synthesizer.speakPlainText("Done", null);
System.out.println("Done.");
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
output.write("public class "+classname);
output.newLine();
output.append("{");
output.newLine();
}
synthesizer.speakPlainText("Press 2 to create main function", null);
System.out.println("Press 2 to create main function");
input=src.nextInt();
if(input==2)
{
synthesizer.speakPlainText("Creating main function", null);
System.out.println("Creating function");
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
Thread.sleep(2000);
synthesizer.speakPlainText("Done", null);
System.out.println("Done.");
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
output.write("public static void main(String args[]) ");
output.newLine();
output.append("{");
output.newLine();
}
synthesizer.speakPlainText("Press 3 to print something", null);
System.out.println("Press 3 to print something");
input=src.nextInt();
if(input==3)
{
synthesizer.speakPlainText("Type something that you want to print", null);
System.out.println("Type something that you want to print:");
String print=src.nextLine();
output.write("System.out.println(\""+print+"\");");
output.close();
}
synthesizer.deallocate();
}
catch(Exception e)
{
}
第2行和第3行执行良好但第3行以后程序终止。 如果我使用next()而不是nextLine()它完美地工作。 如何解决这个问题?
答案 0 :(得分:1)
我认为查看更多代码会很有帮助。我注意到src没有在代码片段中声明。先前使用过src吗?
如果之前使用过,则src的缓冲区可能不为空。例如,.nextInt()会将输入字符留在缓冲区中,因为它不是整数。如果输入字符仍在缓冲区中.nextLine()将其读作空行。您可以尝试在.println()之前调用src.nextLine()并丢弃输入。这将清除缓冲区。
编辑:好的,这就是问题所在:
input=src.nextInt();
你正在输入两个东西,数字和eol字符。 eol字符由.nextInt()保留在缓冲区中,因此下次调用.nextLine()时,它会读取缓冲区,查看eol字符,并假设您刚输入一个空行。因此print =""这里:
String print=src.nextLine();
我会使用两个扫描仪,一个用于整数,一个用于字符串。或者在调用.nextLine()之前清除缓冲区。