# Pure Java random string code to Processing random string code

**URL:** <https://discourse.processing.org/t/pure-java-random-string-code-to-processing-random-string-code/36072>\
**Category:** Coding Questions\
**Created:** [April 1, 2022, 8:19pm UTC](https://discourse.processing.org/t/pure-java-random-string-code-to-processing-random-string-code/36072 "2022-04-01T20:19:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [April 1, 2022, 8:19pm UTC](https://discourse.processing.org/t/pure-java-random-string-code-to-processing-random-string-code/36072/1 "2022-04-01T20:19:12Z")

</div>

how do I make this program in processing rather than pure java?

```auto
// Java program generate a random AlphaNumeric String

// using Math.random() method

 

public class RandomString {

 

     // function to generate a random string of length n

     static String getAlphaNumericString( int n)

     {

 

         // chose a Character random from this String

         String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

                                     + "0123456789"

                                     + "abcdefghijklmnopqrstuvxyz" ;

 

         // create StringBuffer size of AlphaNumericString

         StringBuilder sb = new StringBuilder(n);

 

         for ( int i = 0 ; i < n; i++) {

 

             // generate a random number between

             // 0 to AlphaNumericString variable length

             int index

                 = ( int )(AlphaNumericString.length()

                         * Math.random());

 

             // add Character one by one in end of sb

             sb.append(AlphaNumericString

                           .charAt(index));

         }

 

         return sb.toString();

     }

 

     public static void main(String[] args)

     {

 

         // Get the size n

         int n = 20 ;

 

         // Get and display the alphanumeric string

         System.out.println(RandomString

                                .getAlphaNumericString(n));

     }

}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 1, 2022, 8:53pm UTC](https://discourse.processing.org/t/pure-java-random-string-code-to-processing-random-string-code/36072/2 "2022-04-01T20:53:00Z")

</div>

Hello,

Please post clean formatted code.

Try to extract the function first and then add it to a class if needed:

```auto
void setup()
  {
  int n = 20 ;
  System.out.println(getAlphaNumericString(5));  
  }
  
  String getAlphaNumericString(int n)
    {
   // Code here
    }

```

I was able to do this and get it to work:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/9/e922bdd95714f7ac4652b81c3332d9dccd5332e9.png)

`:)`

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [April 1, 2022, 9:08pm UTC](https://discourse.processing.org/t/pure-java-random-string-code-to-processing-random-string-code/36072/3 "2022-04-01T21:08:05Z")

</div>

```auto
void setup()
{
  int n = 20 ;
  System.out.println(getAlphaNumericString(5));
}

String getAlphaNumericString(int n)
{
  public class RandomString {

    // chose a Character random from this String

    String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

      + "0123456789"

      + "abcdefghijklmnopqrstuvxyz" ;

    // create StringBuffer size of AlphaNumericString

    StringBuilder sb = new StringBuilder(n);

    for ( int i = 0; i < n; i++) {

      // generate a random number between

      // 0 to AlphaNumericString variable length

      int index

        = ( int )(AlphaNumericString.length()

        * Math.random());

      // add Character one by one in end of sb

      sb.append(AlphaNumericString

        .charAt(index));
    }

    return sb.toString();
  }

  public static void main(String[] args)

  {

    // Get the size n

    int n = 20 ;

    // Get and display the alphanumeric string

    System.out.println(RandomString.getAlphaNumericString(n));
  }
}

```

With this code, I get “expecting {, found ‘for’”

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 2, 2022, 9:56am UTC](https://discourse.processing.org/t/pure-java-random-string-code-to-processing-random-string-code/36072/4 "2022-04-02T09:56:04Z")

</div>

You can’t have a class inside a function

function must be before class or inside class

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 2, 2022, 1:56pm UTC](https://discourse.processing.org/t/pure-java-random-string-code-to-processing-random-string-code/36072/5 "2022-04-02T13:56:10Z")

</div>

> [@Chrisir](#):
>
> You can’t have a class inside a function.

Actually Java can have keyword `class` inside a function!

However its usage is rare & not so many folks know about it: 🤓

```auto
void settings() {
  size(600, 400);
  noLoop();

  class classInsideFunction extends PApplet {
    public void settings() {
      size(400, 300);
      println(sketchPath());
    }

    public void setup() {
      frameRate(1);
    }

    public void draw() {
      background((color) random(#000000));
    }
  }

  final String[] switches = {
    ARGS_SKETCH_FOLDER + '=' + sketchPath(), 
    ARGS_LOCATION + "=100,100", 
    "" 
  };

  runSketch(switches, new classInsideFunction());
}

void draw() {
  background((color) random(#000000));
}

void mousePressed() {
  redraw = true;
}

void keyPressed() {
  mousePressed();
}

```
