| | |
Need urgent help! please.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
k kinda figured out that i have to add spaces in between my mathematical expression.. it compiles.. my problem now is that it has some code smells.. i have to clean this job.. can you help me? tnx
How can I make this any clearer? You have two threads. One person has already commented on this thread, probably not knowing about the other. People are donating their time here. Be courteous enough not to waste people's time with duplicate threads.
http://www.daniweb.com/forums/thread146370.html
•
•
•
•
it has some code smells, although it compiles.. can you help me again..
Stack is a raw type. References to generic type Stack<E> should be parameterized
Type safety: The method push(Object) belongs to the raw type Stack. References to generic type Stack<E> should be parameterized
Its working fine with me.
•
•
•
•
Enter the algebraic expression in infix: 4 + 4 * 2
The expression in postfix is: 4 4 2 * +
The answer to the equation is: 12
If im still missing something. Please be more clear

Regards,
PuneetK
•
•
Join Date: Sep 2008
Posts: 91
Reputation:
Solved Threads: 1
•
•
•
•
How can I make this any clearer? You have two threads. One person has already commented on this thread, probably not knowing about the other. People are donating their time here. Be courteous enough not to waste people's time with duplicate threads.
http://www.daniweb.com/forums/thread146370.html
Hmmmmm.. sorry if ever waste your time sir! how do i marked my first thread SOLVED?!
•
•
Join Date: Sep 2008
Posts: 91
Reputation:
Solved Threads: 1
•
•
•
•
Hello again,
Its working fine with me.
Only space is required.
If im still missing something. Please be more clear
Regards,
PuneetK
Hello. thank you very much 4 the help you've given. going back to the code, what if i don't want to include spaces in between.. what changes should i make?
•
•
•
•
Hello. thank you very much 4 the help you've given. going back to the code, what if i don't want to include spaces in between.. what changes should i make?
•
•
•
•
Enter the algebraic expression in infix: 4+4+3*4/2
The expression in postfix is: 4 4 + 3 4 * 2 / +
The answer to the equation is: 14
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; public class Postfix { private static Stack operators = new Stack(); private static Stack operands = new Stack(); public static void main(String argv[]) throws IOException { String infix; //create an input stream object BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in)); //get input from user System.out.print("\nEnter the algebraic expression in infix: "); infix = keyboard.readLine(); //output as postfix System.out.println("The expression in postfix is:" + toPostfix(infix)); //get answer System.out.println("The answer to the equation is: " + evaluate (toPostfix(infix)) + "\n"); } private static String toPostfix(String infix) //converts an infix expression to postfix { String symbol, postfix = ""; for (int i = 0; i < infix.length(); i++) { symbol = infix.charAt(i)+""; //if it's a number, add it to the string if (Character.isDigit(symbol.charAt(0))) postfix = postfix + " " + (Integer.parseInt (symbol)); else if (symbol.equals("(")) //push ( { Character operator = new Character('('); operators.push(operator); } else if (symbol.equals(")")) //push everything back to ( { while (((Character)operators.peek()).charValue() != '(') { postfix = postfix + " " + operators.pop(); } operators.pop(); } else //print operators occurring before it that have greater precedence { while (!operators.empty() && !(operators.peek()).equals("(") && prec(symbol.charAt(0)) <= prec(((Character)operators.peek()).charValue())) postfix = postfix + " " + operators.pop(); Character operator = new Character(symbol.charAt(0)); operators.push(operator); } } while (!operators.empty()) postfix = postfix + " " + operators.pop(); return postfix; } private static int evaluate(String postfix) { StringTokenizer s = new StringTokenizer(postfix); //divides the input into tokens int value; String symbol; while (s.hasMoreTokens()) { symbol = s.nextToken(); if (Character.isDigit(symbol.charAt(0))) //if it's a number, push it { Integer operand = new Integer(Integer.parseInt(symbol)); operands.push(operand); } else //if it's an operator, operate on the previous two operands { int op2 = ((Integer)operands.pop()).intValue(); int op1 = ((Integer)operands.pop()).intValue(); int result = 0; switch(symbol.charAt(0)) { case '*': {result = op1 * op2; break;} case '+': {result = op1 + op2; break;} case '-': {result = op1 - op2; break;} case '/': {result = op1 / op2; break;} case '%': {result = op1 % op2; break;} } Integer operand = new Integer(result); operands.push(operand); } } value = ((Integer)operands.pop()).intValue(); return value; } private static int prec(char x) { if (x == '+' || x == '-') return 1; if (x == '*' || x == '/' || x == '%') return 2; return 0; } }
Used forloop instead of StringTokenizer for toPostfix method.
Regards,
PuneetK
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
Hmmmmmmm.. Sarcastic, aren't we? At the bottom of your thread, you can click "Mark as Solved". Someone has already merged the two threads though, so it's too late. However, when you feel that THIS thread is solved, you can click "Mark as Solved" on it. It'll be in blue letters at the bottom of the thread, right below the last post.
![]() |
Similar Threads
- pls heeeeeeeeelp its urgent. (C)
- Urgent! (C++)
- URGENT: Need help on I/O code! (Java)
- Urgent help (Networking Hardware Configuration)
- Homepage keeps switching back to a porn site (Web Browsers)
- Importing SQL Script File - Urgent !! (Database Design)
Other Threads in the Java Forum
- Previous Thread: I need help with my program
- Next Thread: dynamic dispatch method
Views: 1793 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool keyword linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






