0

I'm currently writing a program that creates a madlib. the program takes a text file as an input and replaces the placeholders with console input from the user. The placeholders are defined in the text file when a word is surrounded by "<" and ">". My program will take the contents of the placeholder and it will ask the user to input whatever word was between the two symbols.

However whenever I run my program it seems to skip the first placeholder in the text file and it goes to the the second and then it properly runs from there. I'm not sure why it does this, I've tried looking at older posts but their answers simply tell them to put another "output.nextLine()" below their code but that only adds onto my issues. does anyone have a solution?

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;
        }

    }
}

text file instructions: 1) copy and paste code these contents into a text file and place it in the project folder 2) when prompted for file input after you enter "c" to create a new madlib, reference the name of the text file and inlucde .txt 3) then enter a name for the new file that will be created to hold the output of the program and include .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> .
  • I looked at that one man, it didn't give me the right answer. – CRSoftware33 Apr 11 '17 at 19:45
  • i suggest pulling line by line and parsing the string returned. it's much easier. – RAZ_Muh_Taz Apr 11 '17 at 19:48
  • 3
    Yes, it IS a duplicate of that other question. This is happening because `next` gets a word and _doesn't_ fetch the newline that comes after that word, as described in the answers to the other question. You really shouldn't mix `next` and `nextLine` like this. – Dawood ibn Kareem Apr 11 '17 at 19:50
  • How would I go about fixing it? – CRSoftware33 Apr 11 '17 at 19:52
  • 1
    Don't mix `next` and `nextLine`. Just use `nextLine` for all your input. – Dawood ibn Kareem Apr 11 '17 at 19:53
  • I use nextLine first becuase it needs to grab the entire line, then I use next to walk the line it just grabbed to check each token, I then use nextLine when the user is inputting to make sure I grab a multiword answer. – CRSoftware33 Apr 11 '17 at 19:56
  • No, I'm not talking about `lineScan`. I mean your calls to `console.next()`. The problem is that you've used both `next` and `nextLine` on `console`, and you need to change `console.next()` to `console.nextLine()`. You can leave all the processing of `lineScan` exactly as it is. – Dawood ibn Kareem Apr 11 '17 at 20:02
  • damn. I've been staring at the wrong part of the program for over a day. That fixed it. I apologize for the duplicate. – CRSoftware33 Apr 11 '17 at 20:05
  • P.S. I like your username – CRSoftware33 Apr 11 '17 at 20:05

0 Answers0