Pure Java random string code to Processing random string code

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

// 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));

     }

}

Hello,

Please post clean formatted code.

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

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:

:)

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’”

You can’t have a class inside a function

function must be before class or inside class

Actually Java can have keyword class inside a function!

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

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();
}
1 Like