سؤال

I am using Fretts api. Below is snippet of code.

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)
    {
    }

Line 2 and 3 are executed well but line 3 onwards program is terminated. If i use next() instead of nextLine() it works perfectly. How to tackle this problem?

هل كانت مفيدة؟

المحلول

I think it would be helpful to see more code. I noticed that src is not declared in the code snippet. Is src used previously?

If it is used previously, it is possible that the buffer for src is not empty. For instance, .nextInt() will leave the enter character in the buffer because it is not an integer. If the enter character is still in the buffer .nextLine() will read it as an empty line. You could just try calling src.nextLine() before .println() and discard the input. That will clear the buffer.

Edit: Alright so this is the problem:

input=src.nextInt();

You are entering two things, the number and the eol character. The eol character is left in the buffer by .nextInt(), so the next time you call .nextLine() it reads the buffer, sees the eol character, and assumes you just inputted an empty line. Therefore print = "" here:

String print=src.nextLine();

I would use two scanners, one for ints, and one for strings. Or just clear the buffer before you call .nextLine().

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top