Hello folks, I have a program that uses a linked list to implement a stack to convert a string from infix notation to postfix notation, and then to evaluate it. Well, the conversion part works just fine, but I have encountered a problem when trying to evaluate it. In my evaluate method, in the switch statement, when I try to compile, I get 4 errors (one for each case): possible loss of precision, found: int, required: char. I have everything set up as a char data type, no ints. I have no idea where it is getting these ints from. Any ideas, would be fantastic.

import javax.swing.JOptionPane;
  
public class InToPost {
	private LinkList list;
	
	private String inString;
	
	private String outString = "";
	
	public InToPost myStack;
	
	public InToPost(String in) {
		inString = in;
		myStack = new InToPost();
	}
	
	public InToPost() {
		list = new LinkList();
	}
	
	public void push(char j) {
		list.insertFront(j);
	}
	
	public char pop() {
		return list.deleteFront();
	}
	
	public boolean isEmpty() {
		return (list.isEmpty());
	}
	
	public void displayStack() {
		System.out.print("Stack: ");
		list.displayList();
	}

class LinkList {
	private Link head;
	
	public LinkList() {
		head = null;
	}
	
	public boolean isEmpty() {
		return (head == null);
	}
	
	public void insertFront(char d) {
		Link newLink = new Link(d);
		newLink.next = head;
		head = newLink;
	}
	
	public char deleteFront() {
		Link temp = head;
		head = head.next;
		return temp.data;
	}
	
	public void displayList() {
		Link current = head;
		while (current != null) {
			current.displayLink();
			current = current.next;
		}
		System.out.println("");
	}
	
	class Link {
		public char data; // data item
	
		public Link next; // next link in list
	
		public Link(char d) {
			data = d;
		}
	
		public void displayLink() {
			System.out.print(data + " ");
		}
	}
}
public String doTrans() {
	for (int j = 0; j < inString.length(); j++) {
		char ch = inString.charAt(j);
		switch (ch) {
			case '+': 
			case '-':
				gotOper(ch, 1); 
				break; //   (precedence 1)
			case '*': // it's * or /
			case '/':
				gotOper(ch, 2); // go pop operators
				break; //   (precedence 2)
			case '(': // it's a left paren
				myStack.push(ch); // push it
				break;
			case ')': // it's a right paren
				gotParen(ch); // go pop operators
				break;
			default: // must be an operand
				outString = outString + ch; // write it to output
				System.out.println(outString);
				break;
		}
	}
	
	while (!myStack.isEmpty()) {
		outString = outString + myStack.pop();
	}
	System.out.println(outString);
	return outString; // return postfix
}
public void evaluate() {
	char a;
	char b;
	for (int j = 0; j < outString.length(); j++) {
		char result = 0;
		char ch = outString.charAt(j);
		switch (ch) {
			case '+':
				a = myStack.pop();
				b = myStack.pop();
				result = a + b;
				myStack.push(result);
				break;
			case '-':
				a = myStack.pop();
				b = myStack.pop();
				result = a - b;
				myStack.push(result);
				break;
			case '*':
				a = myStack.pop();
				b = myStack.pop();
				result = a * b;
				myStack.push(result);
				break;
			case '/':
				a = myStack.pop();
				b = myStack.pop();
				result = a / b;
				myStack.push(result);
				break;
			default:
				myStack.push(ch);
				break;
		}
	}
	System.out.println(myStack.pop());
}

public void gotOper(char opThis, int prec1) {
	while (!myStack.isEmpty()) {
		char opTop = myStack.pop();
		if (opTop == '(') {
			myStack.push(opTop);
			break;
		}// it's an operator
		else {// precedence of new op
			int prec2;
			if (opTop == '+' || opTop == '-')
				prec2 = 1;
			else
			prec2 = 2;
			if (prec2 < prec1) // if prec of new operator is less
			{ //    than prec of old
				myStack.push(opTop); // save newly-popped operator
				break;
			} else
			// prec of new operator is not less than prec of old
			outString = outString + opTop; 
		}
	}
	myStack.push(opThis);
}
public void gotParen(char ch){ 
	while (!myStack.isEmpty()) {
		char chx = myStack.pop();
		if (chx == '(') 
			break; 
		else
		outString = outString + chx; 
	}
}
public static void main(String[] args) {
	String inString = JOptionPane.showInputDialog("Enter your expression: ");
	System.out.println(inString);
	String outString;
	InToPost theTrans = new InToPost(inString);
	outString = theTrans.doTrans();
	System.out.println("Postfix is " +outString+ '\n');
	InToPost calculate = new InToPost(outString);
	calculate.evaluate();
	}
}

Thank you,
jmasta

I've figured out that problem, when you apply +,-,*,/ to two chars it returns an int, instead of a new char. I adjusted my program for that, but now, when I run it, if i put in "1+2" i'll get Postfix is 12+, then Answer = 99. If I just put in '1', i'll get answer = 49. Any ideas where I'm missing the conversion from char to int, since '1' is 49 in unicode?

Actually, I'm gonna scrap the whole thing and start from scratch I think. Consider this thread 'solved' for now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.