title: Java ReplaceAll Problem
body: <p>I am writing a program to solve postfix problems. I am not needing help with the algorithm as I already know it. But I have stumbled across a problem with the replace all function
  In this program we are given operators that should be defined; I stored the definitions in a map. E means this problem is an evaluation and D means this problem is a definition. The definitions could be nested inside of each other. My problem comes when I try to retrieve the definition with the replaceAll function. It works except for on one instance. I will give the input file and my output to show what I mean.</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  class Problem
  {
  
      private static String line;
      private static HashMap&lt;String, String&gt; map = new HashMap();
      private static Stack operandStack = new Stack();
  
      public static void main(String[] args) throws IOException
      {
  
           FileReader fin = new FileReader("postfix.in");
          BufferedReader infile = new BufferedReader(fin);
  
          FileWriter fout = new FileWriter("postfix.out");
          BufferedWriter outfile = new BufferedWriter(fout);
  
          line = infile.readLine();
          do
          {
  
              if(line.substring(0,1).equals("E"))
              {
  
                  line = line.replaceAll("E", "");
                 line =  line.replaceAll("\"+", "");
                 for(int i = 0; i &lt; line.length(); i++)
                 {
  
                     if(map.containsKey(line.substring(i,i+1)) ||
                         line.substring(i, i+1).matches("!|-|!|%|/"))
                     {
                         //check to see if its not a predifined operator
                         if(!line.substring(i, i+1).matches("!|-|!|%|/"))
                         {
                             String operator;
  
                           operator = line.substring(i, i+1);
  
                           //simplifies operators
                           line = line.replaceAll("\\"+operator, map.get(operator));
                         }
  
                     }
                     else if(!line.substring(i,i+1).equals(" "))
                     {
  
                          operandStack.push(line.substring(i,i+1));
                     }
  
                 }
  
                 System.out.println(line);
                 operandStack.clear();
  
              }
              else if(line.substring(0,1).equals("D"))
              {
                  line = line.replaceAll("\"$", "");     //remove quote at end of string
                  map.put(line.substring(1,2), line.substring(3)); //put the definition on the map
              }
  
         //    System.out.println(map);
              line = infile.readLine();
         }while(!line.equals("Q"));
  
          infile.close();
          outfile.close();
  
  
      }
  
  
  
  
  }
  </code></pre>
  
  <p>Here is the input file</p>
  
  <pre><code>D+" 0%--"
  E"1 2+"
  D*" 1%//"
  E"3 4*"
  D@"!!**"
  E"17 4@+6*5/"
  D&amp;"!*"
  D$" 1%/"
  Da"$/"
  D'" 0%-"
  E"28 5&amp;'-$"
  E"3 4$/"
  E"3 4a"
  E"4 5a3/"
  E"4@&amp;"
  E"4&amp;@"
  E"2!!!!****@"
  E"2&amp;&amp;&amp;@"
  Q
  </code></pre>
  
  <p>Output of the code</p>
  
  <pre><code>1 2 0%--
  3 4 1%//
  17 4!! 1%// 1%// 0%--6 1%//5/
  28 5! 1%// 0%-- 1%/
  3 4 1%//
  3 4a      //this is not simplified
  4 5a3/    //this is not simpliied
  4!! 1%// 1%//! 1%//
  4! 1%//!! 1%// 1%//
  2!!!! 1%// 1%// 1%// 1%//!! 1%// 1%//
  2! 1%//! 1%//! 1%//!! 1%// 1%//
  </code></pre>
  
  <p>I believe the solution to this problem lies with fixing this line, but i do not know what to do with it.</p>
  
  <pre><code>line = line.replaceAll("\\"+operator, map.get(operator));
  </code></pre>