-1

I need to create a random string of letters, but have never used the random class before.

here is the spec: Create a small program that will ask the user to enter their name. Store the name in a string Print the name Ask the user to change their last name Manipulate the string to the new last name. Print the new name. Create an infinite loop that will change the string variable randomly. See if your program will crash.

so far I have this

import java.util.Scanner;
import java.util.Random;

public class StringNames {

    public static void main(String[] args) {

        System.out.println("Please enter a name");

        Scanner myInput = new Scanner(System.in);
        String firstName = new String();
        firstName = myInput.next();

        String lastName = new String();
        lastName = myInput.next();

        System.out.println(firstName + " " + lastName);

        System.out.println("Please change your last name");
        lastName = myInput.next();

        System.out.println(firstName + " " + lastName);

        for (;;) {

        }
    }

}

How can I create a random string of letters to change the last name to in the infinite loop? I am unsure how long the string has to be. The simpler the code the better. I am slow.

Thank you for your guidance

Barney
  • 2,355
  • 3
  • 22
  • 37
cat hodges
  • 29
  • 5
  • 6
    Look! There was a [link](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string-in-java?rq=1) off to the right when I hit this page that might be helpful! – Tim Pote Mar 14 '13 at 00:27
  • I did see that link, but I didn't feel comfortable with any of the answers. Thank you though. – cat hodges Mar 14 '13 at 00:54
  • [This answer](http://stackoverflow.com/a/2863888/1690982) might be helpful. – ddmps Mar 14 '13 at 23:50

1 Answers1

1

Firstly, just a little thing:

String lastName=myInput.next();

Is a lot more efficient than creating a string with the class string and then assigning that string a value.

My solution to your problem is:

    import java.util.Scanner;
    import java.util.Random;

public class StringNames {

public static void main(String[] args) {
    Random generator=new Random();
    Scanner myInput = new Scanner(System.in);

    System.out.println("Please enter a name");
    String firstName = myInput.next();

    System.out.println("Please enter a last name");
    String lastName = myInput.next();

    System.out.println("Your name is:");
    System.out.println(firstName + " " + lastName);

    System.out.println("Please change your last name");
    lastName = myInput.next();

    System.out.println(firstName + " " + lastName);
   char[] lastNameChar=lastName.toCharArray();
    for (int i=0;i<lastNameChar.length;i++) {
        int randomNum=generator.nextInt(133);
        lastNameChar[i]=(char)randomNum;

    }
    lastName=String.valueOf(lastNameChar);
    System.out.println(firstName + " " + lastName);
}
}

What this does is that it prompts for the name etc. as you specified. Then there is a for loop. When they change their name, that string is converted into a char array which allows us to get the length and change individual elements of the 'String'. Then we generate a random number to be used as the randomly generated character.

WE then translate that randomly generated number into a char by casting it as such.

What this does is that it immediately changes the value to a character in this ASCII character table: ASCII Characters

We continue for the length of their new last name. Then convert the char array back to a string, and print the string.

In order to refine the range of your number generator (at the minute it generated between 0 and 133) simply add a do while loop:

  do{
        randomNum=generator.nextInt(133);
  }while(randomNum>33 &&randomNum<120);
James
  • 349
  • 1
  • 6
  • 17