As you said, i am geting the error during convertion from String to int. That is because, the String contains special character '&'. I Think, that can't be converted into int. but, i want the entire code as int to proceed further in my program. How to do it ??
Can anyone solve my problem please? i am giving simple java program to do this convertion.
You are posing a mysterious problem. I can't imagine what int value you expect the String "P II + 3630-146 & 3630-337" to be parsed to. Perhaps if you wrote what int value you expected this to produce, forum members would be able to suggest something. Do you expect (3630-146)*(3630-337)?
If you're looking for a numeric identifier for that String then you can use the hashCode() method. This just returns an integer "hashcode" for the string. This code will appear to be an arbitrary value for your string. The only thing you're guarenteed concerning this hashcode is that two equal strings will have the same hashcode.
This is a one-way function. You can't start with the hashcode and get back to the String.
The problem here is that we're using the word 'convert' when what Ashvini is really talking about should be describe with the word 'parse'.
Converting a string into a number implies a 1-to-1 type of relationship (most of the time). But parsing, implies 1-many relationships that can vary depending on your parsing engine. For example, what order of operation do you use in your example? If you go left to right + would be evaluated before & but if you use scientific notation, & would be evaluated before +.
What you want to do is either write your own parser (your 'small' then becomes a big problem as writing a numeric parser is non-trivial) or find a numeric equation parser out there that most closely matches your data and use it by converting your data. For example, other parsers might interpret Pi as "pi" or have special symbols like "@pi".
Oh and as I side note, parsing is never "simple" as you say your program is, even using someone else's parser. Speaking as someone whose done a ton of mathematical programming as well as written a numeric parser from scratch, it can be *very* difficult.
If you think about it, MatLab and Mathematica make millions based solely on their ability to parse equations. In fact, with proper licenses you can actually use their engines to parse your data, but again this is not simple.