Results 1 to 1 of 1
Thread: Trouble with Tokenizing String
- 03-30-2010, 02:19 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 6
- Rep Power
- 0
Trouble with Tokenizing String
Hello! I am working on a program that performs mathematical operations on a complex number. I have all the code compiled and am trying to get through the run-time errors. My hunch is that I am doing something wrong with the fromString method since regardless of how many complex numbers I input (in postfix), it only returns the first complex number. Any ideas?
Thanks! Andy
and the heavy lifting class (with the suspect method):Java Code:package complexnumbers; import complexnumbers.*; import java.util.*; import java.io.*; public class Project2 { public static void main (String[] args){ String charConv = "null"; String more = "null"; char nextChar = '0'; Stack theStack = new Stack(); Scanner keyboard = new Scanner(System.in); do{ //allow user to enter and evaluate any number of expressions System.out.println("Enter a postfix expression of complex numbers (Ex: ( 2 + 3 i ) ( 1 + 10 i ) + ):"); StringTokenizer input = new StringTokenizer(keyboard.nextLine()); //System.out.println(input.countTokens()); while (input.hasMoreTokens()){ charConv = input.nextToken(); nextChar = charConv.charAt(0); switch (nextChar){ case '(': //System.out.println(input.nextToken()); StringTokenizer expression = new StringTokenizer(input.nextToken(")")); Complex newComplex = Complex.fromString(expression); //System.out.println(newComplex.toString()); theStack.push(newComplex); //push the value onto the stack break; case '+': case '-': case '*': //pop two operands from the stack Complex num1 = (Complex)theStack.pop(); Complex num2 = (Complex)theStack.pop(); //evaluate Complex result = evaluate(num1, num2, nextChar); theStack.push(result); break; } } //pop the final result off the stack Complex finalResult = (Complex)theStack.pop(); System.out.println("The result of your input is:" + finalResult.toString()); //System.out.println(input.nextToken()); System.out.println("Evaluate another expression? (Y = Yes):"); more = keyboard.nextLine(); } while (more.equalsIgnoreCase("y")); } static public Complex evaluate(Complex num1, Complex num2, char operator){ Complex result = new Complex(); //evaluate operation if (operator == '+'){ result = Complex.add(num1, num2); } else if (operator == '-'){ result = Complex.subtract(num1, num2); } else { result = Complex.multiply(num1, num2); } return result; } }
Java Code:package complexnumbers; import complexnumbers.*; import java.util.*; import java.io.*; public class Complex{ private double real, imag; private String oper; public Complex(){ } public Complex(double real, double imag, String oper){ this.real = real; this.imag = imag; this.oper = oper; } public static Complex fromString(StringTokenizer tokenizer){ double real, imag; String oper; //System.out.println(tokenizer.nextToken()); real = Double.parseDouble(tokenizer.nextToken()); oper = tokenizer.nextToken(); imag = Double.parseDouble(tokenizer.nextToken()); return new Complex(real, imag, oper); } public String toString(){ return "(" + real + " " + oper + " " + imag + "i" + ")"; } static public Complex add(Complex num1, Complex num2){ num1.real = num1.real + num2.real; num1.imag = num1.imag + num2.imag; return num1; } static public Complex subtract(Complex num1, Complex num2){ num1.real = num1.real - num2.real; num1.imag = num1.imag - num2.imag; return num1; } static public Complex multiply(Complex num1, Complex num2){ int x1 = (int)num1.real; int y1 = (int)num1.imag; int x2 = (int)num2.real; int y2 = (int)num2.imag; int intTemp, iTemp = 0; intTemp = x1 * x2; x1 = x1 * y2; iTemp = y1 * x2; y1 = y1 * y2; y1 = y1 * (-1); x2 = intTemp + y1; y2 = x1 + iTemp; num1.real = x2; num1.imag = y2; return num1; } }
Similar Threads
-
Tokenizing textfile content into several Tables (Float[])
By althair in forum New To JavaReplies: 3Last Post: 12-30-2009, 02:19 AM -
The constructor Person(String, String, Date) is undefined
By fh84 in forum New To JavaReplies: 7Last Post: 11-03-2009, 02:18 AM -
combine string[] into string like perl's join function
By tekberg in forum Advanced JavaReplies: 9Last Post: 02-23-2009, 01:05 PM -
String tokenizing with Scanner
By vijaygk in forum Advanced JavaReplies: 2Last Post: 07-15-2008, 04:44 AM -
Tokenizing with Scanner
By sireesha in forum New To JavaReplies: 3Last Post: 02-05-2008, 08:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks