//This program takes a text file of the poem The Jabberwocky from //Through the Looking-Glass by Lewis Carrol. // // The file jabberwocky.txt has spacing problems and we will fix them // and output to jabberwockyFixed.txt // // Next we will find the last word of each line and save it to rhymes.txt // // Finally we will fix the spacing on jabberwocky.txt and also swap all // a's and o's and save it to jobberwacky.txt import java.io.*; import java.util.*; public class Jabberwocky { public static void main(String[] args) throws FileNotFoundException { // This is the file we will be using as input. // Note: We don't make the Scanner here since each // method needs to start at the beginning of the file // so they have to make a new Scanner on the file. File inputFile = new File("jabberwocky.txt"); // fix the spacing on the file and output it // to jabberwockyFixed.txt fixSpacing(inputFile); // find the last word of each line and output it // to rhymes.txt File rhymesFile = new File("rhymes.txt"); PrintStream outStream = new PrintStream(rhymesFile); lastWord(inputFile, outStream); // you can output to the console instead by using this method call: // lastWord(inputFile, System.out); // fix spacing and swap all a's and o's and output it // to "jobberwacky.txt" PrintStream outStream2 = new PrintStream(new File("jobberwacky.txt")); jobberwacky(inputFile, outStream2); } // fix the spacing on the file and output it to jabberwockyFixed.txt // @param inputFile - The file to read from public static void fixSpacing(File inputFile) throws FileNotFoundException{ // create a file that we will output to File outputFile = new File("jabberwockyFixed.txt"); // create a PrinStream of the file. We will use this // Printstream instead of System.out PrintStream outStream = new PrintStream(outputFile); // You can output to the console instead of the file // by uncommenting this line: // outstream = System.out; //create a Scanner on the file to go over it one line at a time Scanner fileScan = new Scanner(inputFile); // go over each line of the file while(fileScan.hasNext()){ // get the line from the file and make a line Scanner on that line String line = fileScan.nextLine(); Scanner lineScan = new Scanner(line); // go over each "word" (actually token) in the file while(lineScan.hasNext()){ //get the word String word = lineScan.next(); // print out the word that was found with one space to // go between this word and the next one printed outStream.print(word + " "); } outStream.println(); } } // find the last word of each line and output it to the outStream // @param inputFile - The file to read from // @param outStream - The PrintStream to write to (eg. file or System.out) public static void lastWord(File inputFile, PrintStream outStream) throws FileNotFoundException{ //Note: This code is almost the same as fixSpacing Scanner fileScan = new Scanner(inputFile); while(fileScan.hasNext()){ // loop over lines String line = fileScan.nextLine(); Scanner lineScan = new Scanner(line); String lastToken = ""; while(lineScan.hasNext()){ // loop over tokens in the line // We overwrite the lastToken variable each time. // The last token will be the last thing saved in // the variable, so after the while loop, it has // the last token. lastToken = lineScan.next(); } outStream.println(lastToken); } } // fix spacing and swap all a's and o's and output it to the outStream // @param inputFile - The file to read from // @param outStream - The PrintStream to write to (eg. file or System.out) public static void jobberwacky(File inputFile, PrintStream outStream) throws FileNotFoundException{ //Note: This code is almost the same as fixSpacing Scanner fileScan = new Scanner(inputFile); while(fileScan.hasNext()){// loop over lines String line = fileScan.nextLine(); Scanner lineScan = new Scanner(line); while(lineScan.hasNext()){ // loop over tokens in the line // get the next word String word = lineScan.next(); // swap a's and o's // eg. Jabberwocky word = word.replace("a", "@"); // eg. J@bberwocky word = word.replace("o", "a"); // eg. J@bberwacky word = word.replace("@", "o"); // eg. Jobberwacky // Note: If I did just replace("a", "o") then replace("o", "a"), // I would get: Jabberwocky -> Jobberwocky -> Jabberwacky // which is not what I want to end up with. // swap capital A's and O's the same way word = word.replace("A", "@"); word = word.replace("O", "A"); word = word.replace("@", "O"); outStream.print(word + " "); } outStream.println(); } } }