1

I am new to Java and I am struggling with understanding some basic concepts. For example, here, I am trying to write a program which will generate random strings by using the "Random" class in Java: http://docs.oracle.com/javase/6/docs/api/java/util/Random.html

I noticed that the class can generate random integers, so basically, my idea is to generate random integers and just convert them to a string, if this is possible. Here is my code so 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 I really want to do is to take the field "randomNumber" and just turn it into a random number by using the Java class "Random", more specifically the method "nextInt()", and then turn that into a String. But I am not really understanding how to use the field "randomNumber" anywhere, do I even need getters and setters for it? Can I simply use "randomNumber" as an argument in any method?

Sorry if this is confusing, thank you so much for your time!

Nora
  • 1,825
  • 8
  • 31
  • 47
  • You will need getters and or setters if you want to be able to access and or modify your variable from any place that is not inside your class code. If you are only going to be using the variable inside your class, you don't need them. – Tim Mar 24 '14 at 12:11
  • Thank you so much Tim Castelijns! – Nora Mar 24 '14 at 12:17

3 Answers3

2

You have declared a field, but it's not of type Random. It's of type int. To be able to call the nextInt() method, you need an object of type Random, because that's the class where nextInt() is declared.

You also need to return something (the generated string) from the method.

And to be able to call the method from other classes, you need to make the 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 you need to generate a random string, you can simply do

RandomStringGenerator rsg = new RandomStringGenerator();
String s = rsg.generateRandomString();
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

I am not sure, what you exactly want to achieve, but this can help, I added commentary at the end of each line :

public static void main(String[] args) {
    String randomString; //define new String variable
    randomString = generateRandomString(); //call method and store returned values into randomString variable
    System.out.println(randomString); //printing randomString
}


private static String generateRandomString() {
    Random r = new Random(); //You have to create new instance of class Random and name it as you want. I gave it a name "r"
    int randomInt = r.nextInt(); //here you generate random number
    String randomString = String.valueOf(randomInt); //You generated Integer, but you want String, this transform Integer into String
    return randomString; //Now you have random String you can return
}
libik
  • 22,239
  • 9
  • 44
  • 87
0

You haven't used java.util.Random API anywhere in code. You need to create an instance of Random to generate a psedorandom sequence of integers which you can convert to String literals. Please check the below code

   Random random = new Random();
   String randomIntegerString = random.nextInt() + "";

Your code would be

private int randomNumber;

//add a Random variable and instantiate
Random random = new Random();

/**
 *  Method returns a String representation of pseudo random
 *  number generated based on <code> randomNumber </code>
 */
private String generateRandomString(int randomNumber){
    return random.nextInt(randomNumber) + "";
}
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53