The Artima Developer Community
Sponsored Link

Java Answers Forum
Converting String to int

3 replies on 1 page. Most recent reply: Nov 7, 2002 5:53 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Hans Hasne

Posts: 3
Nickname: hanse
Registered: Nov, 2002

Converting String to int Posted: Nov 7, 2002 3:02 PM
Reply to this message Reply
Advertisement
I have generated a random mathproblem with random operators (+-*/) and up to 5 random operators (numbers) and assigned them to a string (for example "a+b-c*d/e")

I am looking for a way to evaluate the expression in the string and assign it to an integer result.

I thought of using the Tokenizer, but I cant figure it out.

Thanks in advance


Hans Hasne

Posts: 3
Nickname: hanse
Registered: Nov, 2002

Re: Converting String to int Posted: Nov 7, 2002 3:11 PM
Reply to this message Reply
This is not part of a homework assignment.
I am trying to learn Java on my own and I need help with the conversion of datatypes.
I tried to use chars but it obviously used the ANSI numbers to represent the operators.

Hans Hasne

Posts: 3
Nickname: hanse
Registered: Nov, 2002

Re: Converting String to int Posted: Nov 7, 2002 3:42 PM
Reply to this message Reply
I know that the Object String is not convertible to int, but please help me write a method that does this or tell me how to use the tokenizer to accomplish it.

Thank you

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Converting String to int Posted: Nov 7, 2002 5:53 PM
Reply to this message Reply
Look at the answer I just posted in the C# forum here (http://www.artima.com/forums/flat.jsp?forum=76&thread=2151). It is in C#, of course, but amazingly (!) similar to what you'd do in Java. There are two additional things (besides changing the language, and API calls, which is pretty easy) you need to do to support the mathematical syntax in your examles:
* First, it looks like you don't require spaces between the operators and operands (which I was using to split them up), so you would need to split things up based on the operators. You could do that with StringTokenizer as you mentioned, or even "manually."
* Second, you are also performing multiple operations per string, which means you need to consider precendence. The the Calculator class in my example can already easily handle a series of operatations instead of just one, since it is using the RPN technique; you could queue up all the values then perform the operations, or perform the operations as you go, since the result of the last operation always remains at the top of the stack. The RPN implementation also makes handling of grouping and precedence a snap, should you choose to add those features.

By the way, use Integer.parseInt() to get an int out of a String, Double.parseDouble() to get a double and so on.

Flat View: This topic has 3 replies on 1 page
Topic: Java Bean to Jsp problem Previous Topic   Next Topic Topic: Difference between an interface and a class

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use