Click here to Skip to main content
15,885,983 members
Home / Discussions / Java
   

Java

 
Questionprogramming Pin
quest4knowledge18-Dec-12 23:15
quest4knowledge18-Dec-12 23:15 
AnswerRe: programming Pin
Richard MacCutchan19-Dec-12 0:00
mveRichard MacCutchan19-Dec-12 0:00 
GeneralRe: programming Pin
quest4knowledge20-Dec-12 6:39
quest4knowledge20-Dec-12 6:39 
AnswerRe: programming Pin
April Fans19-Dec-12 16:40
April Fans19-Dec-12 16:40 
GeneralRe: programming Pin
Richard MacCutchan19-Dec-12 23:11
mveRichard MacCutchan19-Dec-12 23:11 
GeneralRe: programming Pin
quest4knowledge20-Dec-12 6:44
quest4knowledge20-Dec-12 6:44 
GeneralRe: programming Pin
April Fans27-Dec-12 3:54
April Fans27-Dec-12 3:54 
QuestionInfix to Prefix, Incorrect output Pin
SamuelSkenzy15-Dec-12 14:19
SamuelSkenzy15-Dec-12 14:19 
I am trying to create a java program that inputs an infix expression, then gives the output in postfix and prefix. So far the code compiled without errors but the answers for the postfix and prefix are not coming out correctly. Also the outputs come out wrong when i put parenthesis. I think my problem is in the methods for the postfix and infix conversion. This is my code so far:
import java.io.*;
import java.util.*;
public class InToPreToPos{
	private static Stack<Character> operatorStack = new Stack<Character> ();
	private static Stack<Comparable> operandStack = new Stack<Comparable> ();

	private static String toPostfix(String infix){
		StringTokenizer s = new StringTokenizer(infix);
		String symbol, postfix = "";
		while (s.hasMoreTokens()){
			symbol = s.nextToken();
			if (Character.isDigit(symbol.charAt(0))){
				postfix = postfix + " " + (Integer.parseInt(symbol));
			}else if (symbol.equals("(")){
				Character operator = new Character('(');
				operatorStack.push(operator);
			}else if (symbol.equals(")")){
				while (operatorStack.peek().charValue() != '('){
					postfix = postfix + " " + operatorStack.pop();
				}
				operatorStack.pop();
			}else{
				while (!operatorStack.empty() && !(operatorStack.peek()).equals("(") && prec(symbol.charAt(0)) <= prec(operatorStack.peek().charValue()))
					postfix = postfix + " " + operatorStack.pop();
				Character operator = new Character(symbol.charAt(0));
				operatorStack.push(operator);
				}
			}
		while (!operatorStack.empty())
			postfix = postfix + " " + operatorStack.pop();
		return postfix;
	}
	private static String toPrefix(String infix){
		StringTokenizer pre = new StringTokenizer(infix);
 
		String symbol1 , prefix= "";
		while(pre.hasMoreTokens()){
			symbol1 = pre.nextToken();
  
			if(Character.isDigit(symbol1.charAt(0))){
				prefix = prefix+ " " +(Integer.parseInt(symbol1));
			}else if(symbol1.equals("(")){
				Character operand = new Character('(');
				operandStack.push(operand);
			}else if(symbol1.equals(")")){
				while (((Character)operandStack.peek()).charValue()!= '('){
					prefix = prefix + " " + operandStack.pop();
				}
				operandStack.pop();
			}else{
				while(!operandStack.empty()&&!(operandStack.peek()).equals("(")
						&& prec(symbol1.charAt(0))<= prec(((Character)operandStack.peek()).charValue()))
     
					prefix = prefix + " "+ operandStack.pop();
				Character operand = new Character(symbol1.charAt(0));
				operandStack.push(operand);
				}
			}
			while(!operandStack.empty())
				prefix = prefix + " " +operandStack.pop();
			return prefix;
	}
 
	private static int prec(char x){
		if (x == '+' || x == '-'){
			return 1;
		}
		else if (x=='^' && x == '*' || x == '/' || x == '%'){
			return 2;
		}
		else{
			return 0;
			}
	}
	public static void main(String args[]) throws IOException{
		BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in));
		String infix;
		System.out.print("Input a infix: ");
		infix = keyboard.readLine();
		System.out.println("Expression in postfix:" + toPostfix(infix));
		System.out.println("Expression in prefix: " + toPrefix(infix));
 
	}
}

AnswerRe: Infix to Prefix, Incorrect output Pin
TorstenH.16-Dec-12 19:47
TorstenH.16-Dec-12 19:47 
QuestionProblem in scanning from file in Sudoku backtracking Pin
Member 858607213-Dec-12 22:10
Member 858607213-Dec-12 22:10 
SuggestionRe: Problem in scanning from file in Sudoku backtracking Pin
Richard MacCutchan13-Dec-12 22:50
mveRichard MacCutchan13-Dec-12 22:50 
GeneralRe: Problem in scanning from file in Sudoku backtracking Pin
Member 858607213-Dec-12 23:17
Member 858607213-Dec-12 23:17 
GeneralRe: Problem in scanning from file in Sudoku backtracking Pin
Richard MacCutchan13-Dec-12 23:47
mveRichard MacCutchan13-Dec-12 23:47 
GeneralRe: Problem in scanning from file in Sudoku backtracking Pin
Member 858607214-Dec-12 0:30
Member 858607214-Dec-12 0:30 
QuestionHow can I get the use of a female's voice in Java Pin
Showlar13-Dec-12 6:30
Showlar13-Dec-12 6:30 
AnswerRe: How can I get the use of a female's voice in Java Pin
Richard MacCutchan13-Dec-12 7:37
mveRichard MacCutchan13-Dec-12 7:37 
AnswerRe: How can I get the use of a female's voice in Java Pin
TorstenH.16-Dec-12 19:50
TorstenH.16-Dec-12 19:50 
GeneralRe: How can I get the use of a female's voice in Java Pin
April Fans26-Dec-12 17:06
April Fans26-Dec-12 17:06 
Questionj2me Pin
4yee12-Dec-12 7:23
4yee12-Dec-12 7:23 
AnswerRe: j2me Pin
TorstenH.12-Dec-12 22:29
TorstenH.12-Dec-12 22:29 
SuggestionRe: j2me Pin
April Fans26-Dec-12 17:08
April Fans26-Dec-12 17:08 
Questionparse and validation in validation.txt Pin
duka_pa11-Dec-12 11:05
duka_pa11-Dec-12 11:05 
AnswerRe: parse and validation in validation.txt Pin
Richard MacCutchan11-Dec-12 11:16
mveRichard MacCutchan11-Dec-12 11:16 
Questionreading barcode Pin
junaid_ullah7-Dec-12 10:18
junaid_ullah7-Dec-12 10:18 
AnswerRe: reading barcode Pin
Richard MacCutchan7-Dec-12 22:07
mveRichard MacCutchan7-Dec-12 22:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.