Click here to Skip to main content
13,200,729 members (57,366 online)
   

Java

 
GeneralRe: executive java program Pin
OmarSH21-Dec-12 3:43
memberOmarSH21-Dec-12 3:43 
GeneralRe: executive java program Pin
Richard MacCutchan21-Dec-12 4:06
mvpRichard MacCutchan21-Dec-12 4:06 
GeneralRe: executive java program Pin
OmarSH21-Dec-12 8:28
memberOmarSH21-Dec-12 8:28 
GeneralRe: executive java program Pin
Richard MacCutchan21-Dec-12 22:56
mvpRichard MacCutchan21-Dec-12 22:56 
AnswerRe: executive java program Pin
Stephen Dycus21-Dec-12 1:42
memberStephen Dycus21-Dec-12 1:42 
GeneralRe: executive java program Pin
OmarSH21-Dec-12 3:05
memberOmarSH21-Dec-12 3:05 
Questionprogramming Pin
quest4knowledge18-Dec-12 23:15
groupquest4knowledge18-Dec-12 23:15 
AnswerRe: programming Pin
Richard MacCutchan19-Dec-12 0:00
mvpRichard MacCutchan19-Dec-12 0:00 
GeneralRe: programming Pin
quest4knowledge20-Dec-12 6:39
groupquest4knowledge20-Dec-12 6:39 
AnswerRe: programming Pin
April Fans19-Dec-12 16:40
memberApril Fans19-Dec-12 16:40 
GeneralRe: programming Pin
Richard MacCutchan19-Dec-12 23:11
mvpRichard MacCutchan19-Dec-12 23:11 
GeneralRe: programming Pin
quest4knowledge20-Dec-12 6:44
groupquest4knowledge20-Dec-12 6:44 
GeneralRe: programming Pin
April Fans27-Dec-12 3:54
memberApril Fans27-Dec-12 3:54 
QuestionInfix to Prefix, Incorrect output Pin
SamuelSkenzy15-Dec-12 14:19
memberSamuelSkenzy15-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
memberTorstenH.16-Dec-12 19:47 
QuestionProblem in scanning from file in Sudoku backtracking Pin
Member 858607213-Dec-12 22:10
memberMember 858607213-Dec-12 22:10 
SuggestionRe: Problem in scanning from file in Sudoku backtracking Pin
Richard MacCutchan13-Dec-12 22:50
mvpRichard MacCutchan13-Dec-12 22:50 
GeneralRe: Problem in scanning from file in Sudoku backtracking Pin
Member 858607213-Dec-12 23:17
memberMember 858607213-Dec-12 23:17 
GeneralRe: Problem in scanning from file in Sudoku backtracking Pin
Richard MacCutchan13-Dec-12 23:47
mvpRichard MacCutchan13-Dec-12 23:47 
GeneralRe: Problem in scanning from file in Sudoku backtracking Pin
Member 858607214-Dec-12 0:30
memberMember 858607214-Dec-12 0:30 
QuestionHow can I get the use of a female's voice in Java Pin
Showlar13-Dec-12 6:30
memberShowlar13-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
mvpRichard 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
memberTorstenH.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
memberApril Fans26-Dec-12 17:06 
Questionj2me Pin
4yee12-Dec-12 7:23
member4yee12-Dec-12 7:23 
AnswerRe: j2me Pin
TorstenH.12-Dec-12 22:29
memberTorstenH.12-Dec-12 22:29 
SuggestionRe: j2me Pin
April Fans26-Dec-12 17:08
memberApril Fans26-Dec-12 17:08 
Questionparse and validation in validation.txt Pin
duka_pa11-Dec-12 11:05
memberduka_pa11-Dec-12 11:05 
AnswerRe: parse and validation in validation.txt Pin
Richard MacCutchan11-Dec-12 11:16
mvpRichard MacCutchan11-Dec-12 11:16 
Questionreading barcode Pin
junaid_ullah7-Dec-12 10:18
memberjunaid_ullah7-Dec-12 10:18 
AnswerRe: reading barcode Pin
Richard MacCutchan7-Dec-12 22:07
mvpRichard MacCutchan7-Dec-12 22:07 
GeneralRe: reading barcode Pin
junaid_ullah8-Dec-12 4:36
memberjunaid_ullah8-Dec-12 4:36 
GeneralRe: reading barcode Pin
Richard MacCutchan8-Dec-12 22:23
mvpRichard MacCutchan8-Dec-12 22:23 
QuestionHow to validate our signature with our public key? Pin
IICTECH6-Dec-12 19:45
memberIICTECH6-Dec-12 19:45 
AnswerRe: How to validate our signature with our public key? Pin
IICTECH11-Dec-12 19:51
memberIICTECH11-Dec-12 19:51 
Questionwhile signing the same data multiple times does signature vary? Pin
IICTECH6-Dec-12 19:36
memberIICTECH6-Dec-12 19:36 
QuestionRe: while signing the same data multiple times does signature vary? Pin
Richard MacCutchan6-Dec-12 22:32
mvpRichard MacCutchan6-Dec-12 22:32 
AnswerRe: while signing the same data multiple times does signature vary? Pin
IICTECH7-Dec-12 2:32
memberIICTECH7-Dec-12 2:32 
AnswerRe: while signing the same data multiple times does signature vary? Pin
IICTECH11-Dec-12 19:47
memberIICTECH11-Dec-12 19:47 
Questionwhile signing the same data multiple times does signature vary? Pin
syam0716-Dec-12 19:28
membersyam0716-Dec-12 19:28 
AnswerRe: while signing the same data multiple times does signature vary? Pin
Richard MacCutchan6-Dec-12 22:31
mvpRichard MacCutchan6-Dec-12 22:31 
QuestionlookupMultiDocPrintServices and Win32 Pin
J-Cop6-Dec-12 3:11
memberJ-Cop6-Dec-12 3:11 
QuestionJCOMBOBOX Pin
Chrisloys4-Dec-12 2:40
memberChrisloys4-Dec-12 2:40 
AnswerRe: JCOMBOBOX Pin
Richard MacCutchan4-Dec-12 4:52
mvpRichard MacCutchan4-Dec-12 4:52 
GeneralRe: JCOMBOBOX Pin
Chrisloys4-Dec-12 5:36
memberChrisloys4-Dec-12 5:36 
GeneralRe: JCOMBOBOX Pin
Richard MacCutchan4-Dec-12 5:58
mvpRichard MacCutchan4-Dec-12 5:58 
GeneralRe: JCOMBOBOX Pin
Chrisloys4-Dec-12 6:04
memberChrisloys4-Dec-12 6:04 
GeneralRe: JCOMBOBOX Pin
Richard MacCutchan4-Dec-12 6:10
mvpRichard MacCutchan4-Dec-12 6:10 
GeneralRe: JCOMBOBOX Pin
Chrisloys4-Dec-12 6:12
memberChrisloys4-Dec-12 6:12 
GeneralRe: JCOMBOBOX Pin
Richard MacCutchan4-Dec-12 6:34
mvpRichard MacCutchan4-Dec-12 6:34 

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.


Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 21 Oct 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid