title: Preserving line breaks and spacing in file IO
body: <p>I am workig on a pretty neat problem challenge that involves reading words from a .txt file.  The program must allow for ANY .txt file to be read, ergo the program cannot predict what words it will be dealing with.</p>
  
  <p>Then, it takes the words and makes them their "Pig Latin" counterpart, and writes them into a new file.  There are a lot more requirements to this problem but siffice to say, I have every part solved save one...when printng to the new file I am unable to perserve the line spacing.  That is to say, if line 1 has 5 words and then there is a break and line 2 has 3 words and a break...the same must be true for the new file.  As it stands now, it all works but all the converted words are all listed one after the other.</p>
  
  <p>I am interested in learning this so I am OK if you all wish to play coy in your answers.  Although I have been at this for 9 hours so "semi-coy" will be appreaciated as well :)  Please pay close attention to the "while" statements in the code that is where the file IO action is happening.  I am wondering if I need to utilize the nextLine() commands from the scanner and then make a string off that...then make substrings off the nextLine() string to convert the words one at a time.  The substrings could be splits or tokens, or something else - I am unclear on this part and token attempts are giving me compiler arrors exceptions "java.util.NoSuchElementException" - I do not seem to understand the correct call for a split command.  I tried something like String a = scan.nextLine() where "scan" is my scanner var.  Then tried String b = a.split()  no go.  Anyway here is my code and see if you can figure out what I am missing.</p>
  
  <p>Here is code and thank you very much in advance Java gods....</p>
  
  <pre><code>import java.util.*;
  import javax.swing.*;
  import java.io.*;
  import java.text.*;
  
  public class PigLatinTranslator
  {
      static final String ay = "ay"; // "ay" is added to the end of every word in pig latin
  
      public static void main(String [] args) throws IOException
      {
          File nonPiggedFile = new File(...);
          String nonPiggedFileName = nonPiggedFile.getName();
          Scanner scan = new Scanner(nonPiggedFile);  
  
          nonPiggedFileName = ...;
  
          File pigLatinFile = new File(nonPiggedFileName + "-pigLatin.txt"); //references a file that may or may not exist yet
  
          pigLatinFile.createNewFile();
          FileWriter newPigLatinFile = new FileWriter(nonPiggedFileName + "-pigLatin.txt", true);
          PrintWriter PrintToPLF = new PrintWriter(newPigLatinFile);
  
          while (scan.hasNext()) 
          {
              boolean next;
              while (next = scan.hasNext()) 
              {
                   String nonPig = scan.next();
                   nonPig = nonPig.toLowerCase();
                   StringBuilder PigLatWord = new StringBuilder(nonPig);
                   PigLatWord.insert(nonPig.length(), nonPig.charAt(0) );
                   PigLatWord.insert(nonPig.length() + 1, ay);
                   PigLatWord.deleteCharAt(0);
                   String plw = PigLatWord.toString();
  
                   if (plw.contains("!") )
                   {
                       plw = plw.replace("!", "") + "!";
                   }
  
                   if (plw.contains(".") )
                   {
                       plw = plw.replace(".", "") + ".";
                   }
  
                   if (plw.contains("?") )
                   { 
                       plw = plw.replace("?", "") + "?"; 
                   }
  
                   PrintToPLF.print(plw + " ");
              }
  
              PrintToPLF.close();
          }
      }
  }
  </code></pre>