java - Struggling to understand how to use fields in a method -


i new java , struggling understanding basic concepts. example, here, trying write program generate random strings using "random" class in java: http://docs.oracle.com/javase/6/docs/api/java/util/random.html

i noticed class can generate random integers, basically, idea generate random integers , convert them string, if possible. here code far:

package øv2;  import java.util.random;  public class randomstringgenerator {       private int randomnumber;      private void setrandomnumber(int randomnumber){         randomnumber = this.randomnumber;     }      private int getrandomnumber(){         return randomnumber;     }      private string generaterandomstring(int randomnumber){         int randomstring = randomnumber.nextint();     }    } 

what want take field "randomnumber" , turn random number using java class "random", more method "nextint()", , turn string. not understanding how use field "randomnumber" anywhere, need getters , setters it? can use "randomnumber" argument in method?

sorry if confusing, thank time!

you have declared field, it's not of type random. it's of type int. able call nextint() method, need object of type random, because that's class nextint() declared.

you need return (the generated string) method.

and able call method other classes, need make method public:

public class randomstringgenerator {      private random randomnumbergenerator = new random();      public string generaterandomstring() {         int randomnumber = randomnumbergenerator.nextint();         string randomstring = integer.tostring(randomnumber);         return randomstring;     } } 

now, when need generate random string, can do

randomstringgenerator rsg = new randomstringgenerator(); string s = rsg.generaterandomstring(); 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -