0

So I was creating this random string generator:

public class Test {
  public static void main(String[] args) {
  String strings = "abcdefghijklmnopqrstuvwxyz1234567890!@#$%&()ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  Random generator = new Random(System.currentTimeMillis());
  int stringLength = strings.length()-1;
  Character character;
    for (int i = 0; i < stringLength; i++) {
      Double test = new Double(Math.random());
      character = strings.charAt(test.intValue());
      String outputString = outputString.concat(character.toString());
    }
    System.out.println(outputString);
  }
}

I went an compiled it using javac Test.java, and it gave me the error outputString might not have been initialised for lines 14 and 16. So I added the String keyword to line 14, and now it's telling me cannot find symbol: variable outputString

Why is this so?

EDIT:

Okay so I took up the suggestions, and this is the code currently:

public class Test {
  public static int randomInt(int min, int max, long seed) {
    Random rand = new Random(seed);
    int randomNum = rand.nextInt((max-min)+1) - min;
    return randomNum;
  }

  public static void main(String[] args) {
  String strings = "abcdefghijklmnopqrstuvwxyz1234567890!@#$%&()ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  int stringLength = strings.length()-1;
  Character character;
    for (int i = 0; i < stringLength; i++) {
      Double test = new Double(randomInt(0, stringLength, System.currentTimeMillis()));
      character = strings.charAt(test.intValue());
      System.out.print(character);
    }
  }
}

The code runs without errors, but it doesn't print anything. I'm currently using Command Prompt to compile it.

Lee Yi
  • 517
  • 6
  • 17

5 Answers5

2

You are defining the outputString only inside of the for loop. It will not be available outside.

You could output the characters directly:

for (int i = 0; i < stringLength; i++) {
  Double test = new Double(Math.random());
  character = strings.charAt(test.intValue());
  System.out.print(character);
}
System.out.println();

If you insist on concatenating a String in a loop, please use StringBuilder instead.


The next problem you will run into:

Double test = new Double(Math.random());
strings.charAt(test.intValue());

That will always get the the first character. The double will be between 0 (inclusive) and 1 (exclusive).

You need to create a random integer between 0 and the length of your alphabet.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
0

The compiler is correctly telling that outputString is not declared correctly. The outputString is declared within the for loop, but you are trying to print the value outside.

A quick way to fix this would be to declare outputString outside the for loop by having outputString = "";

kuriouscoder
  • 5,394
  • 7
  • 26
  • 40
0

outputString is only defined within the block of the for loop, so once you exit it, it no longer exists. To make sure that your code works, define it outside the loop:

String outputString = "";
for (int i = 0; i < stringLength; i++) {
    Double test = new Double(Math.random());
    character = strings.charAt(test.intValue());
    outputString += character.toString();
}
System.out.println(outputString);
Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
0

It is because you are calling outputString.concat before you have intialized it try

String outputString = "";
outputString = outputString.concat(character.toString());`
Ellery
  • 1,356
  • 1
  • 14
  • 22
0

I managed to get it to work using the following code:

import java.util.Random;

public class Test{

    public static void print(String str) {
      System.out.print(str);
    }

    public static int randomInt(int min, int max) {
        Random rand = new Random(System.currentTimeMillis());
        int randomNum = rand.nextInt((max-min)+1) - min;
        return randomNum;
    }

    public static void main(String[] args) throws InterruptedException {
        String strings = "abcdefghijklmnopqrstuvwxyz1234567890!@#$%&()ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int len = strings.length() - 1;
        StringBuilder outputString = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            Double test = new Double(randomInt(0, len));
            Integer value = test.intValue();
            if (value <= len) {
                //print(value.toString()); For testing purposes
                outputString.append(strings.charAt(value));
                Thread.sleep(1);
            }
            else {i--;}
        }
        print(outputString + "\n");
    }
}
Lee Yi
  • 517
  • 6
  • 17