I am working on a program that computes postfix complex numbers (specifically addition, subtraction, and multiplication) and I keep getting the NumberFormat Exception error when I call my Integer.parseInt() in my fromString method. Below you will see my fromString method and my main method. Any and all help will be greatly appreciated!
COMPLEX CLASS BELOW:
public class Complex
{
private double real, imaginary;
//Constructors
public Complex()
{
this.real=0;
this.imaginary=0;
}
public Complex(double real, double imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
//Observers
public double getReal()
{
return this.real;
}
public double getImaginary()
{
return this.imaginary;
}
//Getters
public void setReal(double real)
{
this.real = real;
}
public void setImaginary(double imaginary)
{
this.imaginary = imaginary;
}
//from String method
public Complex fromString (String tokenizer)throws NumberFormatException
{
try
{ String expression;
String expression1;
expression = tokenizer.substring(0);
System.out.println(expression);
real = Integer.parseInt(expression);
System.out.println(real);
expression1 = tokenizer.substring(2);
imaginary = Integer.parseInt(expression1);
System.out.println(imaginary);
}
catch (NumberFormatException e)
{
System.out.println(e.getMessage());
}
return new Complex(real, imaginary);
}
//to String method
@Override
public String toString()
{
if (this.real == 0)
{
if (this.imaginary == 0)
{
return "0";
}
else
{
return (this.imaginary + "i");
}
}
else
{
if (this.imaginary == 0)
{
return String.valueOf(this.real);
}
else if (this.imaginary < 0)
{
return(this.real + " " + this.imaginary + "i");
}
else
{
return(this.real + " + " + this.imaginary + "i");
}
}
}
//Complex number addition method
public Complex add(Complex a)
{
Complex result = new Complex();
result.setReal(this.real + a.getReal());
result.setImaginary(this.imaginary + a.getImaginary());
return result;
}
//Complex number sutraction method
public Complex subtract(Complex a)
{
Complex result = new Complex();
result.setReal(this.real - a.getReal());
result.setImaginary(this.imaginary - a.getImaginary());
return result;
}
//Complex number multiplication method
public Complex multiply(Complex a)
{
Complex result = new Complex();
result.setReal(this.real * a.getReal() - this.imaginary * a.getImaginary());
result.setImaginary(this.real * a.getImaginary() + this.imaginary * a.getImaginary());
return result;
}
}
__________________________________________________ ___________
MAIN METHOD BELOW:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class cmsc230project2 {
public static void main(String[] args) throws IOException
{
int result;
String expression, token;
StringTokenizer tokenizer;
Complex first = new Complex ();
Complex second = new Complex ();
Complex third = new Complex ();
Stack<String> stack = new Stack<String>();
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
//Get postfix complex number expressions from user
System.out.println("Enter a postfix expression of complex numbers:");
expression = stdin.readLine();
//Use tokenizer to break up input data for processing
tokenizer = new StringTokenizer(expression, "( i )", false);
while (tokenizer.hasMoreElements())
{
token = tokenizer.nextToken();
stack.push(token);
//first.fromString(tokenizer);
String a = stack.pop();
Complex exp1=first.fromString(a);
}
}
}