473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.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);

}


}
}
Mar 26 '10 #1
1 2967
jkmyoung
2,057 Expert 2GB
Could you give a few quick examples of the input you're expecting into the program?
Mar 29 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two...
1
by: stf | last post by:
Hello, I use c# to start excel and to get numberformat of active cell. VBA outputs results as expected, but via c# i get result 'Standard' instead of 'General' for a call to numberformat. Why?...
2
by: kiran | last post by:
Hi, I am able to create instance of Culture Info for different cultures. How will I get number format as "$ ###,###,##0.00" from CultureInfo.NumberFormat. I need it in this format so that I...
24
by: ru | last post by:
I got an error message from a client running my software, and after building a test machine with the exact regional settings as my client I could reproduce the bug. The setting causing the bug...
2
by: arne.wiklund | last post by:
Is there an error in the currentCulture.NumberFormat.NumberDecimalSeparator? Take a look at this code: Thread.CurrentThread.CurrentCulture = new CultureInfo("nb-NO");...
1
by: schaf | last post by:
Hi NG! I have a .NET project which uses a sublibrary written in C++. On the .NET side I use the XMLSerializer to serialize an object (into an MemoryStream). This stream would be casted into a...
4
by: Stanislaw Tristan | last post by:
Problem: I created a custom exception that inherits from Exception and added a some of new properties. All new properties filled in the constructors. When I throwing this custom exception and go...
9
by: Frawls | last post by:
Hi I Am am having problems with a stored Procedure that i wrote. Basically whats happening is that the Stored procedure Runs fine when i EXECUTE it in SQL Query analyzer. But when i debug...
10
by: John Nagle | last post by:
Here are three network-related exceptions. These were caught by "except" with no exception type, because none of the more specific exceptions matched. This is what a traceback produced: 1....
4
by: MaTe | last post by:
hi, I have tried to automate excel (2007) in vb.net and i have problem with function NumberFormat. I want set the thousand separator for range: Range("C1:C6").NumberFormat = "0,0.00" and it...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.