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

  • SEARCH
  • Home
  • 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 7553581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:05:59+00:00 2026-05-30T11:05:59+00:00

I am supposed to write program to convert infix notation to postfix notation using

  • 0

I am supposed to write program to convert infix notation to postfix notation using stacks in Java. I’m pretty much done, but am getting an error. Here is the code as of now:

import java.util.Scanner;
import java.util.Stack;

public class InfixToPostfix {
  private String infix;
  private String postfix = "";

  public void convertString (String a){
    String str = "";
    infix = a;
    Stack<String> stack = new Stack<String>();

    for (int i = 0; i < infix.length(); i++){
      str = infix.substring(i,i+1);
      if(str.matches("[a-zA-Z]|\\d"))
        postfix += str;
      else if (isOperator(str)){
        if (stack.isEmpty()){
          stack.push(str);
        }
        else{
          String stackTop = stack.peek();
          while (getPrecedence(stackTop,str).equals(stackTop)
                 && !(stack.isEmpty())){
            postfix += stack.pop();
            if (!(stack.isEmpty()))
              stackTop = stack.peek();
          }
          stack.push(str);
        }
      }
    }
    while(!(stack.isEmpty()))
      postfix += stack.pop();
    System.out.println("The postfix form of the expression you entered is: " +
                       postfix);
  }

  private boolean isOperator(String ch){
    String operators = "*/%+-";
    if (operators.indexOf(ch) != -1)
      return true;
    else
      return false;
  }

  private String getPrecedence(String op1, String op2){
    String multiplicativeOps = "*/%";
    String additiveOps = "+-";
    if ((multiplicativeOps.indexOf(op1) != -1) && (additiveOps.indexOf(op2) != -1))
      return op1;
    else if ((multiplicativeOps.indexOf(op2) != -1) && (additiveOps.indexOf(op1) != 
                                                        -1))
      return op2;
    else if((multiplicativeOps.indexOf(op1) != -1) && (multiplicativeOps.indexOf   
                                                       (op2) != -1))
      return op1;
    else
      return op1;
  }
  public static void main(String[] args) {

    System.out.println("Enter an expression in the Infix form:");
    Scanner scanner = new Scanner(System.in);

    String expression = scanner.nextLine();
    new convertString (expression);

  } 
}

The error is on the very last line and says:

“Exception in thread “main” java.lang.Error: Unresolved compilation problem:
convertString cannot be resolved to a type

at InfixToPostfix.main(InfixToPostfix.java:62)"

Any ideas as to how to fix this? What am I doing wrong?

EDIT: I got my code working and it is successfully converting infix to postfix, but is there any way I can get it to also evaluate the expression and spit out the answer? For example if the input was 2+3 then it would convert it to 23+ and then spit out 5 as well.

  • 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-30T11:06:00+00:00Added an answer on May 30, 2026 at 11:06 am

    You don’t provide a constructor that accepts a string.

    What you should do given the code you have written, is

    new InfixToPostfix().convertString(expression);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I write a program using immutable data structures in Java. Even though it
I am supposed to write a Java program to sum the following sequence: 1.0/1.0
I am supposed to write a program in JavaScript to find all the anagrams
I am supposed to write a program that gets some PNG images from the
Java is supposed to be write once, run anywhere and it really can be,
I need to write a java script. This is supposed to validate if the
I'm trying to write a secure transfer file program using Python and AES and
I am supposed to write a program in C only. It's code for a
I am trying to write a python program using Python 3 I have to
I have to write an address book program in C# 2008. It is supposed

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.