-2

Well I'm recently started to learn java coding, and there are some bumps that needs to be resolved.

I just have encountered this issue that when converting a string with special parameters inside (e.g "3^5+7*10" or "(1/2)"). in such occasions, the Integer.parseInt returns an error saying that the number is not supported. And the actual code is:

int i = Integer.parseInt("5*3^2+3^2");

Thanks in advance :)

Update: Is there another library that I could use in order to do the conversion, AFAIK this used to be very easy in C#.

lkn2993
  • 566
  • 7
  • 26
  • Why do you expect a simple `parseInt` (which means "convert this *number* to an `int`") to be able to handle entire expressions with calculations? – Joachim Sauer Feb 27 '13 at 16:28

4 Answers4

4

You cannot send an expression like that into parseInt. It is designed to convert a given String representation of a number, into an Integer. You would have to get the solution to your expression and then pass the result into parseInt. Although if you get the solution in the java code it will be a number by that point anyway.

Kevin D
  • 3,564
  • 1
  • 21
  • 38
  • So What should I do?( Also please note that I can't simply calculate the string prior to conversion. Either there should be a way to convert the string to the actual int( which does calculations in place) or there should be a way to calculate the string and then convert it to int.) – lkn2993 Feb 27 '13 at 17:51
  • Why can't you simply calculate the result? Is this mathematical expression a user input? If you edit your question with a bit more of what you are actually trying to achieve, and what your constraints are people may be able to help you get a solution rather than being in the current position of only being able to say "You are using it wrong." – Kevin D Feb 28 '13 at 09:16
  • You said that I should evaluate the string which is not normally possible. Breaking the string to tokens and any other kinds of non straight evaluation is also not possible(should not be done). Besides my own answer, could you please provide me a useful alternative? – lkn2993 Feb 28 '13 at 10:48
2

You are going to have to write code parse the string yourself and evaluate it, I'd personally break it into tokens and evaluated it.

Alternatively here is an example of someone doing that using Java's scripting capabilities to do just this. Algebra equation parser for java

Community
  • 1
  • 1
Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238
  • Already answered my own question. + your answer is wrong since breaking the string to tokens is NOT an option. – lkn2993 Feb 28 '13 at 10:41
0

This is because parseInt, will parse a correct numeric String value. This is an entire math expression.

String cannot calculates math operations in its inside, because its only characters what it knows and anything else.

Garis M Suero
  • 7,974
  • 7
  • 45
  • 68
0

Ok, found the solution myself. With JDK 1.6 you can use JavaScript, which is just as powerful as the C# in such conversions. So bad that using java is such a pain like that. I think there's a reason most programmers use C++. You can view the actual answer here, also don't forget to upvote that.

Community
  • 1
  • 1
lkn2993
  • 566
  • 7
  • 26