title: Java - Reversing words in a string
body: <p>This is our code:    </p>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.util.*;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  public class reverse {
  
  public static void main(String[] args) throws FileNotFoundException {
      File fil = new File("textFile.txt");
      Scanner scan = new Scanner(fil);
  
      while (scan.hasNext()) {
          String in = scan.nextLine();
          in = new StringBuffer(in).reverse().toString();
          Pattern replace = Pattern.compile("\\W+");
          Matcher matcher = replace.matcher(in);
          System.out.println(matcher.replaceAll("\t"));
          }
  
  
  
      }
  
  }
  </code></pre>
  
  <p>In our textFile.txt we have letters, numbers and words in different lines. We want print them out in reverse order. So if one line is: Yes, 4 Chocolate milk. We want to print out: milk Chocolate 4 Yes. </p>
  
  <p>Our code prints out words in reverse, so yes becomes sey. We don't want it to be like this, but don't know how to change it. We've been thinking about creating a stack, but we don't how it'll work together with out Pattern and Matcher stuff.</p>