• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generating Random operators for an array of operands  RSS feed

 
Paresh Nayak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the requirement :-

- Store some numbers in an array whose size is decided at run time.
- Generate random operators and insert them between operands.
- Print the result(if possible the equation also).


Can someone help me out with the code?
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you done so far?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paresh Nayak wrote:Here is the requirement :-

- Store some numbers in an array whose size is decided at run time.
- Generate random operators and insert them between operands.
- Print the result(if possible the equation also).


Can someone help me out with the code?


A couple of things to note:

1) This is your homework, so it is your responsibility to do it. Having somebody else do it for you would be unethical, and would deprive you of the chance to learn.

2) This site is NotACodeMill (⇐ click me). Even if it were ethical for someone to do it for you, it goes against the purpose of this site.

3) If you ShowSomeEffort (⇐ click) and TellTheDetails (⇐ click) about where you're getting stuck, you'll get some excellent help here.

Good luck, and welcome to the Ranch!
 
Paresh Nayak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well first of all. Its not a homework and I'm not a student. I was solving math puzzles that appear daily in the news paper and got this idea.
As for your second point. I understand and I'm sorry. But honestly I am working on it.
This is what I've done.


Help me out at line no 30
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paresh Nayak wrote:Well first of all. Its not a homework and I'm not a student. I was solving math puzzles that appear daily in the news paper and got this idea.


Fair enough. Nevertheless, presumably you're doing this because you want to learn, and having somebody code it for you will only hinder your learning process.


Help me out at line no 30


What's wrong about it? What do you expect to happen and what is happening instead? If you're getting an error, either at compile time or at runtime, copy/paste the exact, complete error message.

 
Paresh Nayak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enter n:
3
2 4 6
Exception in thread "main" java.lang.NumberFormatException: For input string: "2*4"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at RandomOperation.main(RandomOperation.java:30)

its giving me NumberFormatException as you see here.

I want the randomly generated operator to be placed in the equation. values[j] and values[++j] are operands.
int result = Integer.parseInt(values[j]+operator+values[++j]);
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paresh Nayak wrote:Enter n:
3
2 4 6
Exception in thread "main" java.lang.NumberFormatException: For input string: "2*4"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at RandomOperation.main(RandomOperation.java:30)

its giving me NumberFormatException as you see here.


Well, the error message is telling you exactly what's going wrong. You can't pass "2*4" to parseInt() (as clearly indicated in the documentation: http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String)).

If you expected it to evaluate 2*4 to get 8, that's not what parseInt() is for, as clearly indicated by the docs. You'd need an expression parsing library, or a scripting plugin.

If you expected it to just pull off the 2, it doesn't work that way, again, as indicated by the docs. You'll need to break up the input string yourself.

 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you use the operator from your code you are just passing any old string to java so what it does is
convert values[j] to a string + operator + values[++j] to a string.

you then ask it to convert this string to an integer, which is where the number format exception comes from. It doesn't know the string "+" is any different from the string "hello" so it treats it exactly the same.

you need to convert the string "+" to the coda operator + and the same for /-*
 
Paresh Nayak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan, Thank you for your useful info. I finally got it. I downloaded expression parsing library from Here, did some modification in my code and used the evaluate method from the lib.
Couldn't have done it without you.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paresh Nayak wrote:

You don't have to create a new Random every time you call the method. In fact, it will slow things down and overwork the garbage collector.
Random is a random number generator, and it's Thread-safe; so you usually only have to create it once, viz:However, it should be pointed out that static variables are the exception rather than the rule, and there is really no reason for your 'exp' and 'j' variables to be defined that way.

I'm presuming that the reason you did is because you had problems when you didn't, since the only place they get used is in main().
If that's the case, just put them in the method.

HIH

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paresh Nayak wrote:Jeff Verdegan, Thank you for your useful info. I finally got it. I downloaded expression parsing library from Here, did some modification in my code and used the evaluate method from the lib.
Couldn't have done it without you.


Glad you got it sorted out! And welcome to the Ranch!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
You don't have to create a new Random every time you call the method. In fact, it will slow things down and overwork the garbage collector.


And it can make things less random.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!