473,396 Members | 2,033 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,396 software developers and data experts.

Help With Expression Solver

I am trying to create a program that takes string expressions and solves them mathematically (i.e. string "5 + 4 * 2" will evaluate as 13). i have this much of the program made, but I am not sure why it is not working. it keeps telling me there is a "Number Format Exception"

Please help!

Expand|Select|Wrap|Line Numbers
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import static java.lang.Integer.*;
  4. import static java.lang.System.*;
  5. import java.lang.*;
  6.  
  7. class ExpSolver
  8. {
  9.     private ArrayList<String> list;
  10.     private String expression;
  11.     private String trimmedExp;
  12.     private String evaluated;
  13.     private int eval;
  14.  
  15.     public ExpSolver(String s)
  16.     {
  17.         list=new ArrayList<String>();
  18.         expression=s;
  19.         trimmedExp=s.trim();
  20.         for (int x=0; x<trimmedExp.length(); x++)
  21.         {
  22.             list.add(""+trimmedExp.charAt(x));
  23.         }
  24.     }
  25.  
  26.     public void setExpression(String s)
  27.     {
  28.         expression=s;
  29.         trimmedExp=s.trim();
  30.         list.clear();
  31.         for (int x=0; x<trimmedExp.length(); x++)
  32.         {
  33.             list.add(""+trimmedExp.charAt(x));
  34.         }        
  35.     }
  36.  
  37.     public void solveExpression()
  38.     {
  39.         eval=0;
  40.         for (int x=0; x<list.size(); x++)
  41.         {
  42.             if (list.get(x).equals("*")||list.get(x).equals("/"))
  43.             {
  44.                 if (list.get(x).equals("*"))
  45.                 {
  46.                     list.set(x-1, String.valueOf(Integer.parseInt(list.get(x-1))*Integer.parseInt(list.get(x+1))));
  47.                     list.remove(x+1);
  48.                     list.remove(x);
  49.  
  50.                 }
  51.  
  52.                 else if(list.get(x).equals("/"))
  53.                 {
  54.                     list.set(x-1, String.valueOf(Integer.parseInt(list.get(x-1))/Integer.parseInt(list.get(x+1))));
  55.                     list.remove(x+1);
  56.                     list.remove(x);
  57.  
  58.                 }
  59.             }
  60.  
  61.         }
  62.  
  63.         for (int x=0; x<list.size(); x++)
  64.         {
  65.             if (list.get(x).equals("-"))
  66.             {
  67.                 list.set(x-1, String.valueOf(Integer.parseInt(list.get(x-1))-Integer.parseInt(list.get(x+1))));
  68.                 list.remove(x);
  69.                 list.remove(x+1);
  70.             }
  71.  
  72.             else if (list.get(x).equals("+"))
  73.             {
  74.                 list.remove(x);
  75.             }
  76.         }
  77.         for (int x=0; x<list.size(); x++)
  78.         {
  79.             if (list.get(x).equals(" "))
  80.             {
  81.                 list.remove(x);
  82.             }
  83.  
  84.             list.set(x, list.get(x).trim());
  85.         }
  86.  
  87.         for (int x=0; x<list.size(); x++)
  88.         {
  89.             eval+=Integer.parseInt(list.get(x));            
  90.         }
  91.  
  92.         evaluated=String.valueOf(eval);
  93.  
  94.  
  95.     }
  96.  
  97.     public String toString( )
  98.     {
  99.         return evaluated;
  100.     }
  101. }
  102.  
  103. public class Lab04b
  104. {
  105.     public static void main( String args[] )
  106.     {
  107.         ExpSolver test = new ExpSolver("3 + 5");
  108.         test.solveExpression();
  109.         out.println(test.toString());
  110.  
  111.         test.setExpression("3 * 5");
  112.         test.solveExpression();
  113.         out.println(test);
  114.  
  115.         test.setExpression("3 - 5");
  116.         test.solveExpression();
  117.         out.println(test);
  118.  
  119.         test.setExpression("3 / 5");
  120.         test.solveExpression();
  121.        out.println(test);
  122.  
  123.        test.setExpression("5 * 5 + 2 / 2 - 8 + 5 * 5 - 2");
  124.         test.solveExpression();
  125.         out.println(test);
  126.     }
  127. }
  128.  
  129.  
Feb 1 '07 #1
3 7735
r035198x
13,262 8TB
I am trying to create a program that takes string expressions and solves them mathematically (i.e. string "5 + 4 * 2" will evaluate as 13). i have this much of the program made, but I am not sure why it is not working. it keeps telling me there is a "Number Format Exception"

Please help!

Expand|Select|Wrap|Line Numbers
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import static java.lang.Integer.*;
  4. import static java.lang.System.*;
  5. import java.lang.*;
  6.  
  7. class ExpSolver
  8. {
  9.     private ArrayList<String> list;
  10.     private String expression;
  11.     private String trimmedExp;
  12.     private String evaluated;
  13.     private int eval;
  14.  
  15.     public ExpSolver(String s)
  16.     {
  17.         list=new ArrayList<String>();
  18.         expression=s;
  19.         trimmedExp=s.trim();
  20.         for (int x=0; x<trimmedExp.length(); x++)
  21.         {
  22.             list.add(""+trimmedExp.charAt(x));
  23.         }
  24.     }
  25.  
  26.     public void setExpression(String s)
  27.     {
  28.         expression=s;
  29.         trimmedExp=s.trim();
  30.         list.clear();
  31.         for (int x=0; x<trimmedExp.length(); x++)
  32.         {
  33.             list.add(""+trimmedExp.charAt(x));
  34.         }        
  35.     }
  36.  
  37.     public void solveExpression()
  38.     {
  39.         eval=0;
  40.         for (int x=0; x<list.size(); x++)
  41.         {
  42.             if (list.get(x).equals("*")||list.get(x).equals("/"))
  43.             {
  44.                 if (list.get(x).equals("*"))
  45.                 {
  46.                     list.set(x-1, String.valueOf(Integer.parseInt(list.get(x-1))*Integer.parseInt(list.get(x+1))));
  47.                     list.remove(x+1);
  48.                     list.remove(x);
  49.  
  50.                 }
  51.  
  52.                 else if(list.get(x).equals("/"))
  53.                 {
  54.                     list.set(x-1, String.valueOf(Integer.parseInt(list.get(x-1))/Integer.parseInt(list.get(x+1))));
  55.                     list.remove(x+1);
  56.                     list.remove(x);
  57.  
  58.                 }
  59.             }
  60.  
  61.         }
  62.  
  63.         for (int x=0; x<list.size(); x++)
  64.         {
  65.             if (list.get(x).equals("-"))
  66.             {
  67.                 list.set(x-1, String.valueOf(Integer.parseInt(list.get(x-1))-Integer.parseInt(list.get(x+1))));
  68.                 list.remove(x);
  69.                 list.remove(x+1);
  70.             }
  71.  
  72.             else if (list.get(x).equals("+"))
  73.             {
  74.                 list.remove(x);
  75.             }
  76.         }
  77.         for (int x=0; x<list.size(); x++)
  78.         {
  79.             if (list.get(x).equals(" "))
  80.             {
  81.                 list.remove(x);
  82.             }
  83.  
  84.             list.set(x, list.get(x).trim());
  85.         }
  86.  
  87.         for (int x=0; x<list.size(); x++)
  88.         {
  89.             eval+=Integer.parseInt(list.get(x));            
  90.         }
  91.  
  92.         evaluated=String.valueOf(eval);
  93.  
  94.  
  95.     }
  96.  
  97.     public String toString( )
  98.     {
  99.         return evaluated;
  100.     }
  101. }
  102.  
  103. public class Lab04b
  104. {
  105.     public static void main( String args[] )
  106.     {
  107.         ExpSolver test = new ExpSolver("3 + 5");
  108.         test.solveExpression();
  109.         out.println(test.toString());
  110.  
  111.         test.setExpression("3 * 5");
  112.         test.solveExpression();
  113.         out.println(test);
  114.  
  115.         test.setExpression("3 - 5");
  116.         test.solveExpression();
  117.         out.println(test);
  118.  
  119.         test.setExpression("3 / 5");
  120.         test.solveExpression();
  121.      out.println(test);
  122.  
  123.      test.setExpression("5 * 5 + 2 / 2 - 8 + 5 * 5 - 2");
  124.         test.solveExpression();
  125.         out.println(test);
  126.     }
  127. }
  128.  
  129.  
You don't have to import classes in the java.lang package.
The numberFormat exception occurs when you try to convert a string to an Integer (in this case) where the string does not represent a number. You should debug your program with println statements to verify that you are accessing the correct indices if the list because you are currently trying to convert a symbol like +, or / to an int.
Feb 2 '07 #2
dmjpro
2,476 2GB
yes he is right ...
perhaps ur code is trying to parseInt a string which has a character like arithmatic operator....
Feb 3 '07 #3
rengaraj
168 100+
At one point of your program's execution, Integer.parseInt() method gets an argument that is not a valid integer. That's why NumberFormatException is thrown.

Run the program and observe the stack trace for looking into it. You may find out the example for which the program is crashing by running the program for each individual example. Then simulate the code for that example with paper and pencil. I hope you will be able to come up with the mistake you have done.

There are pretty good algorithms for solving arithmetic expressions. You may use one if you find your algorithm incorrect.
Feb 18 '07 #4

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

Similar topics

4
by: Rex_chaos | last post by:
Hi all, I am looking for a high-performance container(matrix and vector) for numerical computation. Someone recommended the boost::multi_array. After having looked at the documentation, I know...
18
by: talin at acm dot org | last post by:
I've been reading about how "lambda" is going away in Python 3000 (or at least, that's the stated intent), and while I agree for the most part with the reasoning, at the same time I'd be sad to see...
0
by: Josh Golden | last post by:
has anyone every used the solver add-in for Excel? it allows you to solve linear and non-linear problems given certain conditions. I was wondering if something like this exists already in .NET? ...
3
by: Mr.Doubt | last post by:
I'm trying to run a Excel macro, which uses SOLVER.XLA Add-In, in VB.NET application. When the macro is executed I get the following error message "Solver: An unexpected internal error occured,...
11
by: ago | last post by:
Inspired by some recent readings on LinuxJournal and an ASPN recipe, I decided to revamp my old python hack... The new code is a combination of (2) reduction methods and brute force and it is quite...
0
by: engsolnorm | last post by:
A co-worker and I want to increase our knowledge of Python. I have a tiny bit of exposure to python. He has none, but has wide experience with C, C++, NET, C#, etc. We agreed we'd do a Sudoku...
3
by: raajagopal.v | last post by:
hallo comp.lang.c i am a student trying to sole the bisection method in numerical methods to find the root of any equation,i need the coding because i dont know how to implement the polynomial...
8
by: Rony Steelandt | last post by:
Hi all, I wonder if somebody had a Sudoko solver written in Python ? Rony
1
by: T.Crane | last post by:
Hi, OK, I'm trying to figure out how to use the ODE solver (scipy.integrate.ode.ode). Here's what I'm doing (in iPython) y0 = dt = 0.01 tEnd = 12 t0 = 0 Y = zeros()
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.