NumberFormat Exception errors! Ugh!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JavaNut
    New Member
    • Mar 2010
    • 1

    NumberFormat Exception errors! Ugh!

    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.parseIn t() 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(do uble imaginary)
    {
    this.imaginary = imaginary;
    }

    //from String method
    public Complex fromString (String tokenizer)throw s NumberFormatExc eption
    {
    try
    { String expression;
    String expression1;

    expression = tokenizer.subst ring(0);
    System.out.prin tln(expression) ;

    real = Integer.parseIn t(expression);
    System.out.prin tln(real);

    expression1 = tokenizer.subst ring(2);

    imaginary = Integer.parseIn t(expression1);
    System.out.prin tln(imaginary);
    }
    catch (NumberFormatEx ception e)
    {
    System.out.prin tln(e.getMessag e());
    }


    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.rea l + " " + this.imaginary + "i");
    }
    else
    {
    return(this.rea l + " + " + this.imaginary + "i");
    }
    }
    }
    //Complex number addition method
    public Complex add(Complex a)
    {
    Complex result = new Complex();
    result.setReal( this.real + a.getReal());
    result.setImagi nary(this.imagi nary + a.getImaginary( ));
    return result;
    }
    //Complex number sutraction method
    public Complex subtract(Comple x a)
    {
    Complex result = new Complex();
    result.setReal( this.real - a.getReal());
    result.setImagi nary(this.imagi nary - a.getImaginary( ));
    return result;
    }
    //Complex number multiplication method
    public Complex multiply(Comple x a)
    {
    Complex result = new Complex();
    result.setReal( this.real * a.getReal() - this.imaginary * a.getImaginary( ));
    result.setImagi nary(this.real * a.getImaginary( ) + this.imaginary * a.getImaginary( ));
    return result;
    }
    }

    _______________ _______________ _______________ _______________ _
    MAIN METHOD BELOW:


    import java.io.Buffere dReader;
    import java.io.IOExcep tion;
    import java.io.InputSt reamReader;
    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 InputStreamRead er(System.in));

    //Get postfix complex number expressions from user
    System.out.prin tln("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.hasM oreElements())
    {
    token = tokenizer.nextT oken();
    stack.push(toke n);
    //first.fromStrin g(tokenizer);

    String a = stack.pop();
    Complex exp1=first.from String(a);

    }


    }
    }
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Could you give a few quick examples of the input you're expecting into the program?

    Comment

    Working...