I am trying to code a password generator in Java, and I know exactly the way in which I want to accomplish this. The problem I have is that I am unsure how I can achieve my desired goal. 

I want to use a for loop to search through a string, grab a random character, and store that character in the program's memory. I then want to repeat this procedure until the string contains the number of characters specified by the user, and print the resulting string to the terminal. 

How can I do this in a way that is simple and straightforward? 

Attempt 1:

```
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
/**
 * Write a description of class PasswordGenerator here.
 *
 * @author C.G.Stewart
 * @version 06/06/18
 */
public class PasswordGenerator
{
    private String input;
    private int i;
    private String newPass;
    /**
     * Constructor for objects of class Password
     */
    public PasswordGenerator()
    {
        // initialise instance variables
        input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        ArrayList<String> Password = new ArrayList<String>();
        Scanner pass = new Scanner(System.in);
    }

    /**
     * This method generates a random alphanumeric string to be used as the new 
     * password
     */
    public void generatePassword()
    {
        Random rnd = new Random();
        for(i=1; i<=20; i++)
        {
            Math.random();
            System.out.println(input.charAt(i));
        }
        //newPass = System.out.println(input.charAt(i));
    }

    /**
     * This method takes the previously generated random alphanumeric string,
     * and outputs it to the screen. 
     */
    public void newPassword()
    {
        System.out.println(newPass);
    }
}

```

Attempt 2:

```
import java.util.Scanner;
import java.util.Random;
/**
 * Write a description of class Password here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Password
{
    // instance variables - replace the example below with your own
    private String index;

    /**
     * Constructor for objects of class Password
     */
    public Password()
    {
        // initialise instance variables
        index="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Scanner pass = new Scanner(System.in);
    }

    //Returns a random alphanumeric string of an inputted length
    public void printPassword()
    {
        for(int i=10; i<=20; i++)
        {
            while(i<=20)
            {
                Random rand = new Random();
                char letter;

                letter = index.charAt(i);
            }
            System.out.println(i);
        }
    }
}

```