This is an infix to postfix converter using java. This is what I have done so fa
ID: 3774384 • Letter: T
Question
This is an infix to postfix converter using java. This is what I have done so far and it won't run after inputting my infix expression. This is the error I get,
Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Unknown Source)
at java.util.Stack.pop(Unknown Source)
at InToPost.convert(InToPost.java:62)
at InToPost.main(InToPost.java:103)
My code:
import java.util.Scanner;
import java.util.Stack;
public class InToPost
{
public static boolean isLetterOrNumber(char ch)
{
if (ch >= '0' && ch <= '9')
{
return true;
}
else if (ch >= 'a' && ch <= 'z')
{
return true;
}
else if (ch >= 'A' && ch <= 'Z')
{
return true;
}
else
return false;
}
public static boolean isLowerPrecedence(char op1, char op2)
{
if (op1 == '+' || op1 == '-')
{
return !(op2 == '+' || op2 == '-');
}
else if (op1 == '*' || op1 == '/')
{
return op2 == '^' || op2 == '(';
}
else if (op1 == '^')
{
return op2 == '(';
}
else if (op1 == '(')
{
return true;
}
else
{
return false;
}
}
public static String convert(String infix)
{
Stack<Character> stack = new Stack<Character>();
String postfix = "";
int infixLength = infix.length();
for (int i = 0; i < infixLength; i++)
{
if(infix.charAt(i) == ' ' || infix.charAt(i) == ',')
{
continue;
}
else if(isLetterOrNumber(infix.charAt(i)))
{
while(!stack.isEmpty() && stack.pop() != '('
&& isLowerPrecedence(stack.pop(), infix.charAt(i)));
{
postfix += stack.pop();
stack.pop();
}
stack.push(infix.charAt(i));
}
else if(isLetterOrNumber(infix.charAt(i)))
{
postfix += infix.charAt(i);
}
else if (infix.charAt(i) == '(')
{
stack.push(infix.charAt(i));
}
else if(infix.charAt(i) == ')')
{
while(!stack.isEmpty() && stack.pop() != '(')
{
postfix += stack.pop();
stack.pop();
}
stack.pop();
}
}
while(!stack.isEmpty())
{
postfix += stack.pop();
stack.pop();
}
return postfix;
}
public static void main(String args[])
{
// create a new scanner for user input
Scanner keyboard = new Scanner(System.in);
// new string for user input of infix notation expression
String infix;
// prompts the user for an input
System.out.println("Enter an expression in infix form: ");
// takes the input and sets it too variable String infix
infix = keyboard.next();
// output given infix notation
System.out.println("Expression in Infix Notation: " + infix);
// sets a new string variable postfix after calling the convert method
// to convert infix input to postfix
String postfix = convert(infix);
// outputs the new postfix expression
System.out.println("Expression in Postfix Notation: " + postfix);
}
}
Explanation / Answer
import java.util.Scanner;
import java.util.Stack;
public class HelloWorld{
public static boolean isOperator(char c)
{
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^'
|| c == '(' || c == ')';
}
public static boolean isLowerPrecedence(char op1, char op2)
{
switch (op1)
{
case '+':
case '-':
return !(op2 == '+' || op2 == '-');
case '*':
case '/':
return op2 == '^' || op2 == '(';
case '^':
return op2 == '(';
case '(':
return true;
default:
return false;
}
}
public static String convert(String infix)
{Stack<Character> stack = new Stack<Character>();
StringBuffer postfix = new StringBuffer(infix.length());
char c;
for (int i = 0; i < infix.length(); i++)
{
c = infix.charAt(i);
if (!isOperator(c))
{
postfix.append(c);
}
else
{
if (c == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
{
postfix.append(stack.pop());
}
if (!stack.isEmpty())
{
stack.pop();
}
}
else
{
if (!stack.isEmpty() && !isLowerPrecedence(c, stack.peek()))
{
stack.push(c);
}
else
{
while (!stack.isEmpty() && isLowerPrecedence(c, stack.peek()))
{
Character pop = stack.pop();
if (c != '(')
{
postfix.append(pop);
} else {
c = pop;
}
}
stack.push(c);
}
}
}
}
while (!stack.isEmpty()) {
postfix.append(stack.pop());
}
return postfix.toString();
}
public static void main(String args[])
{
// create a new scanner for user input
Scanner keyboard = new Scanner(System.in);
// new string for user input of infix notation expression
String infix;
// prompts the user for an input
System.out.println("Enter an expression in infix form: ");
// takes the input and sets it too variable String infix
infix = keyboard.next();
// output given infix notation
System.out.println("Expression in Infix Notation: " + infix);
// sets a new string variable postfix after calling the convert method
// to convert infix input to postfix
String postfix = convert(infix);
// outputs the new postfix expression
System.out.println("Expression in Postfix Notation: " + postfix);
}
}
*******OUTPUT**********
Enter an expression in infix form:
a+b
Expression in Infix Notation: a+b
Expression in Postfix Notation: ab+
*******OUTPUT***********
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.