• Post Reply Bookmark Topic Watch Topic
  • New Topic

Madlibs-style game file i/o.  RSS feed

 
Annalily Blake
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've gotten sort of stuck on my assignment for my Java class and I was wondering if anyone could point me in the right direction.

The general idea of what is intended is that the computer will read an exterior file, and every time it stops at something like < noun > it will ask the reader basically "please input a: noun", so I first want it to find the instance of a < word > and read it, and then I want to be able to change that word to what the user inputted by creating a new file of this new story.

So the original file would read 'Fred Meyers is < adjective >
The console would say Please input a: adjective
The user would input a word (let's say smart)
And a new file would get the words as they're printed, so it would be Fred Meyers is smart in the new file.



I'm stuck on both being able to print out the < noun > without including the characters around it, I am also stuck on how to replace it and write a new document (Although I think that I am supposed to reassign the string in the if loop to the new word, and there should probably be PrintStream somewhere.)

Edit: So I figured out how to print the < noun > thing (and edited the above code), for all except the word with the space (i.e. <your name> ), So I need help figuring out how to get the <your name> to show up, and still on how to print out the story in a new file as I go.

Edit2: Figured out how to print out <your name> using a nested if loop (again, edited the above code to show my current code) but I am still stuck on how to replace it and print it out to a file.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Are you reading < adjective > or <adjective>? The two are very different, particularly if you use a Scanner with its default delimiter of whitespace.
There are all sorts of ways you could do it. These depend on the exact specification and whether < can occur anywhere else in your file.
  • Read the line and use the index of methods on the String.
  • Use the finding methods of Scanner.
  • Read the entire line and use a regex to find a match.
  • Change the delimiter on your Scanner (might not work at all).
  • Split the String on <…> tags (again might not work).
  • There are bound to be several other ways to do it.

    Don't try to read and replace at this moment. Write a little program which reads from your file and simply displays the locatoin of the <…> tags. Then you can confirm you are reading them correctly. Then you will have a method you can use in the real thing.
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another possibility: split the String on < and > only.
     
    Annalily Blake
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is no space between the >< symbols, so it is <adjective>, I want the new word inputted by the user to replace the <adjective> and then the entire thing will be printed to a new document. So I'm not exactly sure what I'm supposed to do and how to fix my code.

    Likewise, I've tried to do something similar but using FileWriter in the hopes that it'll input into a new file:
    However it still doesn't print out to a new file. What should I do to fix that or should I continue trying with the above code.
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Forget about printing things to a new file, for the time being. Get the reading working first. Then the parsing, then the replacement. Print those intermediate stages to System.out. When you have that working, then consider file writing.

    I am not convinced that your Scanner will recognise the tokens correctly, because I think you are not using the right delimiters. Read the Scanner documentation to find about delimiters.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!