• Post Reply Bookmark Topic Watch Topic
  • New Topic

random method in Math class not found?  RSS feed

 
Jason Szewczyk
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a simple program using the random() class in the Math class. Should I be importing something because it can't find the method...

Math.random()

gives me a "cannot find symbol" error... Any ideas?
 
Lionel Kingsley
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just use Math.random() in your program you need not import any package because Math class in defined in java.lang which will be imported by default in all java programs

for example the following program will work fine without importing any package.




But the following program requires you to import java.util package because Random class is defined in that package. either give java.util.* or java.util.Random.

 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason, welcome to the Ranch.

Santhanam Lakshmipathy is quite right; you don't need to import the Math class. BTW Look at the description of its random() method in the API specification: click FRAMES if not already shown, then find "Math" in the lower left frame, then "METHODS." Then you can see exactly what sort of result the random() method returns.

Because you don't appear to have an error in the Math.random() bit, it is quite possible the error is elsewhere. Check out this URL for a list of compiler errors. Make sure you have got all your identifiers spelt absolutely right; people get into no end of difficulty because they miss out a capitalised letter, etc.

Then tell us whether it works.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it also might help if you posted the relevant section of code, and the full, exact text of the error message.
 
Jason Szewczyk
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand everything you are saying and that's exactly what I thought but I continue to get an error. Here is my exact code just to test this method.



The error I get is "cannot find symbol - symbol: method random() location: Math class". Any ideas?
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I copied and pasted your code and it compiled and ran first time. Don't know what is going wrong.
Try checking your classpath and path, using these installation instructions.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason --

You've likely written, at some time in the near or distant past, a class named "Math", and your "Math.java" or compiled "Math.class" files are still around, somewhere. You'll need to delete those. Search around carefully until you find them, because they're definitely there, someplace.

To quickly test my theory, change Math.random() to java.lang.Math.random(), and your program should start compiling correctly. If it does, then I was right and you need to find and delete your own "Math" class.
 
Jason Szewczyk
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

You hit the nail on the head with that one. I had forgotten an old program I wrote for a class in which I defined a Math class. Thanks for the help!!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest, i sit in awe of your diagnostic abilities...
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To avoid these types of problems and others, each project should be in its own folder.
 
Alex Preston
Greenhorn
Posts: 1
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created an account because I wanted to say thanks. I was having this same exact issue and this was the only place with the answer I needed.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Alex.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!