Steffan Harris
Steffan Harris

Reputation: 9326

Java ReplaceAll Problem

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.

import java.io.*;
import java.util.*;

class Problem
{

    private static String line;
    private static HashMap<String, String> 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 < 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();


    }




}

Here is the input file

D+" 0%--"
E"1 2+"
D*" 1%//"
E"3 4*"
D@"!!**"
E"17 4@+6*5/"
D&"!*"
D$" 1%/"
Da"$/"
D'" 0%-"
E"28 5&'-$"
E"3 4$/"
E"3 4a"
E"4 5a3/"
E"4@&"
E"4&@"
E"2!!!!****@"
E"2&&&@"
Q

Output of the 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%//

I believe the solution to this problem lies with fixing this line, but i do not know what to do with it.

line = line.replaceAll("\\"+operator, map.get(operator));

Upvotes: 0

Views: 1533

Answers (1)

Nico Huysamen
Nico Huysamen

Reputation: 10417

See if this works. Replace your concerned line:

line = line.replaceAll("\\"+operator, map.get(operator));

with the following:

line = line.replaceAll(
           Pattern.quote(operator),
           Matcher.quoteReplacement(map.get(operator)));

It produces the output :

1 2 0%--
3 4 1%//
17 4!! 1%// 1%// 0%--6 1%//5/
28 5! 1%// 0%-- 1%/
3 4 1%//
3 4$/
4 5$/3/
4!! 1%// 1%//! 1%//
4! 1%//!! 1%// 1%//
2!!!! 1%// 1%// 1%// 1%//!! 1%// 1%//
2! 1%//! 1%//! 1%//!! 1%// 1%//

which seems ok, but I haven't looked extensively to see if it is correct.

Upvotes: 1

Related Questions