Click here to Skip to main content
   

Java

 
GeneralRe: executive java program PinmemberOmarSH21-Dec-12 4:43 
GeneralRe: executive java program PinmvpRichard MacCutchan21-Dec-12 5:06 
GeneralRe: executive java program PinmemberOmarSH21-Dec-12 9:28 
GeneralRe: executive java program PinmvpRichard MacCutchan21-Dec-12 23:56 
AnswerRe: executive java program PinmemberStephen Dycus21-Dec-12 2:42 
GeneralRe: executive java program PinmemberOmarSH21-Dec-12 4:05 
Questionprogramming Pingroupquest4knowledge19-Dec-12 0:15 
AnswerRe: programming PinmvpRichard MacCutchan19-Dec-12 1:00 
GeneralRe: programming Pingroupquest4knowledge20-Dec-12 7:39 
AnswerRe: programming PinmemberApril Fans19-Dec-12 17:40 
GeneralRe: programming PinmvpRichard MacCutchan20-Dec-12 0:11 
GeneralRe: programming Pingroupquest4knowledge20-Dec-12 7:44 
GeneralRe: programming [modified] PinmemberApril Fans27-Dec-12 4:54 
QuestionInfix to Prefix, Incorrect output PinmemberSamuelSkenzy15-Dec-12 15: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 PinmemberTorstenH.16-Dec-12 20:47 
QuestionProblem in scanning from file in Sudoku backtracking PinmemberMember 858607213-Dec-12 23:10 
SuggestionRe: Problem in scanning from file in Sudoku backtracking PinmvpRichard MacCutchan13-Dec-12 23:50 
GeneralRe: Problem in scanning from file in Sudoku backtracking PinmemberMember 858607214-Dec-12 0:17 
GeneralRe: Problem in scanning from file in Sudoku backtracking PinmvpRichard MacCutchan14-Dec-12 0:47 
GeneralRe: Problem in scanning from file in Sudoku backtracking PinmemberMember 858607214-Dec-12 1:30 
QuestionHow can I get the use of a female's voice in Java PinmemberShowlar13-Dec-12 7:30 
AnswerRe: How can I get the use of a female's voice in Java PinmvpRichard MacCutchan13-Dec-12 8:37 
AnswerRe: How can I get the use of a female's voice in Java PinmemberTorstenH.16-Dec-12 20:50 
GeneralRe: How can I get the use of a female's voice in Java [modified] PinmemberApril Fans26-Dec-12 18:06 
Questionj2me Pinmember4yee12-Dec-12 8:23 
AnswerRe: j2me PinmemberTorstenH.12-Dec-12 23:29 
SuggestionRe: j2me [modified] PinmemberApril Fans26-Dec-12 18:08 
Questionparse and validation in validation.txt Pinmemberduka_pa11-Dec-12 12:05 
AnswerRe: parse and validation in validation.txt PinmvpRichard MacCutchan11-Dec-12 12:16 
Questionreading barcode Pinmemberjunaid_ullah7-Dec-12 11:18 
AnswerRe: reading barcode PinmvpRichard MacCutchan7-Dec-12 23:07 
GeneralRe: reading barcode Pinmemberjunaid_ullah8-Dec-12 5:36 
GeneralRe: reading barcode PinmvpRichard MacCutchan8-Dec-12 23:23 
QuestionHow to validate our signature with our public key? PinmemberIICTECH6-Dec-12 20:45 
AnswerRe: How to validate our signature with our public key? PinmemberIICTECH11-Dec-12 20:51 
Questionwhile signing the same data multiple times does signature vary? PinmemberIICTECH6-Dec-12 20:36 
QuestionRe: while signing the same data multiple times does signature vary? PinmvpRichard MacCutchan6-Dec-12 23:32 
AnswerRe: while signing the same data multiple times does signature vary? PinmemberIICTECH7-Dec-12 3:32 
AnswerRe: while signing the same data multiple times does signature vary? PinmemberIICTECH11-Dec-12 20:47 
Questionwhile signing the same data multiple times does signature vary? Pinmembersyam0716-Dec-12 20:28 
AnswerRe: while signing the same data multiple times does signature vary? PinmvpRichard MacCutchan6-Dec-12 23:31 
QuestionlookupMultiDocPrintServices and Win32 [modified] PinmemberJ-Cop6-Dec-12 4:11 
QuestionJCOMBOBOX PinmemberChrisloys4-Dec-12 3:40 
AnswerRe: JCOMBOBOX PinmvpRichard MacCutchan4-Dec-12 5:52 
GeneralRe: JCOMBOBOX PinmemberChrisloys4-Dec-12 6:36 
GeneralRe: JCOMBOBOX PinmvpRichard MacCutchan4-Dec-12 6:58 
GeneralRe: JCOMBOBOX PinmemberChrisloys4-Dec-12 7:04 
GeneralRe: JCOMBOBOX PinmvpRichard MacCutchan4-Dec-12 7:10 
GeneralRe: JCOMBOBOX PinmemberChrisloys4-Dec-12 7:12 
GeneralRe: JCOMBOBOX PinmvpRichard MacCutchan4-Dec-12 7:34 

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

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


Advertise | Privacy | Mobile
Web04 | 2.8.150224.1 | Last Updated 27 Feb 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid