Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3343684
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:58:59+00:00 2026-05-18T00:58:59+00:00

I am writing a program to solve postfix problems. I am not needing help

  • 0

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));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:59:00+00:00Added an answer on May 18, 2026 at 12:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program that sends an email out at a client's specific local
I am writing a program which has two panes (via CSplitter ), however I
If you are writing a program that is executable from the command line, you
I'm writing a program (for Mac OS X, using Objective-C) and I need to
I'm writing a program to read from a POP3 mailbox and upload the email
I'm writing a program that uses SetWindowRgn to make transparent holes in a window
I am writing a program in Python that will act as a server and
I'm writing a program that contains a generational garbage collector. There are just two
I am writing a program that will draw a solid along the curve of
I am writing a program that does a lot of writes to a Postgres

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.