为什么我的程序跳过第一个nextLine()但是它正确地通过其余的?

时间:2017-04-11 19:42:15

标签: java java-io

我目前正在编写一个创建madlib的程序。程序将文本文件作为输入,并用来自用户的控制台输入替换占位符。当一个单词被"<"包围时,占位符在文本文件中定义。和">"。我的程序将获取占位符的内容,它将要求用户输入两个符号之间的任何单词。

然而,每当我运行我的程序时,它似乎跳过文本文件中的第一个占位符,它转到第二个占位符然后从那里正确运行。我不确定为什么会这样做,我已经尝试过查看较旧的帖子,但他们的答案只是告诉他们放另一个" output.nextLine()"低于他们的代码,但这只会增加我的问题。有没有人有解决方案?

CODE:

public class MadLib {
    public static void main(String[] args)throws IOException{
        Scanner console = new Scanner(System.in);
        Scanner console1 = new Scanner(System.in);
        introduction();
        game(console, console1);
    }

    public static void introduction(){
        System.out.println("Welcome to the game of Mad Libs.\n" +
        "I will ask you to provide various words\n" +
        "and phrases to fill in a story.\n" +
        "The result will be written to an output file.");
    }

    public static void game(Scanner console, Scanner console1)throws IOException{
        System.out.print("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
        String answer = console.next();
        answer =  answer.toLowerCase();
        if(answer.equals("c")){
            System.out.print("Input file name: ");
            String fileName = console.nextLine();
            File file = new File(fileName);
            while(!file.exists()){
                fileName = console.next();
                file = new File(fileName);
                if(!file.exists()){
                    System.out.print("File not found. Try again: ");
                }
            }
            System.out.print("Output file name: ");
            String outFileName = console1.nextLine();
            File outFile = new File(outFileName);
            PrintStream out = new PrintStream(outFile);
            Scanner lineScan = new Scanner(file);
            String s = "";
            while (lineScan.hasNext()){
                String token = lineScan.next();
                if(token.charAt(0) == '<' && token.charAt(token.length()-1) == '>') {
                    String placeHolder = token;
                    String newWord = generator(console, token, placeHolder);
                    s = s + newWord + " ";
                }
                else{
                token = token;
                s = s + token + " ";
                }
            }
            out.println(s+"\n");
            System.out.println("Your mad-lib has been created!");
            game(console, console1);
        }

        if(answer.equals("v")){
            System.out.print("Input file name: ");
            String fileName = console.nextLine();
            File file = new File(fileName);
            while(!file.exists()){
                fileName = console.next();
                file = new File(fileName);
                if(!file.exists()){
                    System.out.print("File not found. Try again: ");
                }
            }
            Scanner input = new Scanner(file);
            while(input.hasNext()){
                String word = input.nextLine();
                System.out.println(word);
            }
            game(console, console1);
        }

        if(answer.equals("q")){
            System.exit(0);
        }
        else{
            game(console, console1);
        }
    }

    public static String checkPlaceHolder(Scanner console, Scanner lineScan, String token) throws IOException {
    if(lineScan.hasNext() && (token.startsWith("<") && token.endsWith(">"))) {
        String placeHolder = token;
        return placeHolder;
    }
        token = token;
        return token;
    }
    public static String generator(Scanner console, String token, String placeHolder) throws IOException{
        String word = placeHolder.replace("<", "").replace(">", ": ").replace("-",  " ");
        if (String.valueOf(word.charAt(0)).equalsIgnoreCase("a") || String.valueOf(word.charAt(0)).equalsIgnoreCase("e") || String.valueOf(word.charAt(0)).equalsIgnoreCase("i") || String.valueOf(word.charAt(0)).equalsIgnoreCase("o") || String.valueOf(word.charAt(0)).equalsIgnoreCase("u")) {
            String prevWord = "an ";
            System.out.print("Please type " + prevWord + word);
            String newWord = console.nextLine();
            return newWord;
        }
        else {
            String prevWord = "a ";
            System.out.print("Please type " + prevWord + word);
            String newWord = console.nextLine();
            return newWord;
        }

    }
}

文本文件说明: 1)将这些内容复制并粘贴到文本文件中,并将其放在项目文件夹中 2)在输入&#34; c&#34;之后提示输入文件时要创建一个新的madlib,请引用文本文件的名称和inlucde .txt 3)然后输入将创建的新文件的名称以保存程序的输出并包括.txt

simple.txt:

I wannabe a <job> when I grow up.
Just like my dad.
Life is <adjective> like that

tarzan.txt:

One of the most <adjective> characters in fiction is named
"Tarzan of the <plural-noun> ." Tarzan was raised by a/an
<noun> and lives in the <adjective> jungle in the
heart of darkest <place> . He spends most of his time
eating <plural-noun> and swinging from tree to <noun> .
Whenever he gets angry, he beats on his chest and says,
" <funny-noise> !" This is his war cry. Tarzan always dresses in
<adjective> shorts made from the skin of a/an <noun>
and his best friend is a/an <adjective> chimpanzee named
Cheetah. He is supposed to be able to speak to elephants and
<plural-noun> . In the movies, Tarzan is played by <person's-name> .

0 个答案:

没有答案