Results 1 to 4 of 4
Thread: Need help with a java program
- 12-12-2011, 04:38 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
Need help with a java program
Hi, I have to convert this program (getValue method) to set it to use double inputs instead of int. I've been trying to make changes in it but not getting proper values as the string entered is being converted into the double value by the getValue method. I'm in a very urgent situation and please help me to get it written properly. Thanks.
Java Code:import java.util.Stack; public class Expression { private String str; private Stack<Integer> st2 = new Stack<Integer>(); private int j = 0; private void getValue() { int sign = -1; int nextNumber = 0; int prevNumber = 0; int value = 0; boolean numeric = true; if (str.charAt(j) == '+') { sign = 1; } else if (str.charAt(j) == '-') { sign = -1; } j++; while (numeric == true) { value += Character.getNumericValue(str.charAt(j)); if (j != (str.length()-1) ) { nextNumber = Character.getNumericValue(str.charAt(j+1)); if (nextNumber >= 0 && nextNumber <=9) { value = value*10; j++; } else { numeric = false; } } else { numeric = false; } } if (sign == -1) { value = value*-1; } else if (sign == 1) { value = value * +1; } st2.push(value); } private String formatString(String str) { String temp = ""; Stack<Character> st3 = new Stack<Character> (); char ch = ' '; int tempNumber = 0; ch = str.charAt(0); if (ch != '+' && ch != '-') { st3.push('+'); } for (int i = 0; i < str.length(); i++) { ch = str.charAt(i); st3.push(ch); if (ch == '*' || ch == '/') { tempNumber = Character.getNumericValue(str.charAt(i+1)); if (tempNumber >= 0 && tempNumber<=9) { st3.push('+'); } } } while(st3.isEmpty() == false) { temp += st3.pop(); } str = new StringBuffer(temp).reverse().toString(); return str; } private void multiply() { int left = 0; int right = 0; int value = 0; j++; getValue(); right = st2.pop(); left = st2.pop(); Multiply m = new Multiply(left,right); value = m.evaluate(); st2.push(value); } private void divide() { int left = 0; int right = 0; int value = 0; j++; getValue(); right = st2.pop(); left = st2.pop(); Divide d = new Divide(left,right); value = d.evaluate(); st2.push(value); } public void handle(String st) { int result = 0; this.str = formatString(st); char current = ' '; for (j = 0; j < str.length(); j++) { current = str.charAt(j); if (current == '+' || current == '-') { getValue(); } if (current == '*') { multiply(); } if (current == '/') { divide(); } } while(st2.isEmpty() == false) { result += st2.pop(); } System.out.println(result); } }
- 12-12-2011, 07:09 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Need help with a java program
Can you show us what you have tried so far. Then we can comment easily.
- 12-13-2011, 10:21 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 13
- Rep Power
- 0
Re: Need help with a java program
Hi, thanks for your reply. I managed to get it working. I'm sorry that I could not mention it here. btw, its nice to see that u too from SL.
- 12-14-2011, 05:13 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Need help with a java program
If you feel that the solution is something interesting, then post it here. Your solution may helpful to others.
BTW, nice to see you too, from SL.
Similar Threads
-
Call one Java Program from another Java Program
By rajpalparyani in forum New To JavaReplies: 3Last Post: 02-14-2011, 04:13 AM -
Is There A Way To Call Another Java Program Within A Java Program
By SwissR in forum New To JavaReplies: 4Last Post: 07-30-2010, 12:25 PM -
execute java program within java program
By popey in forum New To JavaReplies: 2Last Post: 10-22-2009, 05:32 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks