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.
import java.util.*; import java.io.*; public class StoryGenerator { public static void main(String[] args) { readFile(); } public static void readFile() { double sum = 0.0; try { System.out.println("Enter the name of your story file:"); Scanner scan = new Scanner(System.in); Scanner file = new Scanner(new File(scan.next())); while (file.hasNext()) { String input = file.next(); if (input.startsWith("<") && input.endsWith(">")) { String word = input.substring(1, input.length() - 1); System.out.println("Please input a: " + word); } } } catch (java.io.FileNotFoundException ex) { System.out.println("File not found."); System.exit(-1); } } }
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, 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:
Okay, I got "your name" to show up by using a nested if loop.
import java.util.*; import java.io.*; public class StoryGenerator { public static void main(String[] args) { readFile(); } public static void readFile() { double sum = 0.0; try { System.out.println("Enter the name of your story file:"); Scanner scan = new Scanner(System.in); Scanner file = new Scanner(new File(scan.next())); while (file.hasNext()) { String input = file.next(); if (input.startsWith("<") && input.endsWith(">")) { String word = input.substring(1, input.length() - 1); System.out.println("Please input a: " + word); } if (input.startsWith("<your")) { String word = input.substring(1, input.length()); System.out.print("Please input a: " + word); input = file.next(); if (input.startsWith("name>")) { word = input.substring(0, input.length() - 1); System.out.println(" " + word); } } } } catch (java.io.FileNotFoundException ex) { System.out.println("File not found."); System.exit(-1); } } }
Now I need help figuring out how to replace that input and then print the story out to a new file (as stated above.)
EDIT: Figured out how to replace the input, now I just need to know how to PrintStream as the story goes on.
current code:
import java.util.*; import java.io.*; public class StoryGenerator { public static void main(String[] args) { readFile(); } public static void readFile() { double sum = 0.0; try { System.out.println("Enter the name of your story file:"); Scanner scan = new Scanner(System.in); Scanner file = new Scanner(new File(scan.next())); while (file.hasNext()) { String input = file.next(); if (input.startsWith("<") && input.endsWith(">")) { String word = input.substring(1, input.length() - 1); System.out.println("Please input a: " + word); input.replaceAll(input, scan.next()); } if (input.startsWith("<your")) { String word = input.substring(1, input.length()); System.out.print("Please input a: " + word); input = file.next(); input.replaceAll(input, ""); if (input.startsWith("name>")) { word = input.substring(0, input.length() - 1); System.out.println(" " + word); input.replaceAll(input, scan.next()); } } } } catch (java.io.FileNotFoundException ex) { System.out.println("File not found."); System.exit(-1); } } }