-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

3
  • 6
    Look! There was a link off to the right when I hit this page that might be helpful! Commented Mar 14, 2013 at 0:27
  • I did see that link, but I didn't feel comfortable with any of the answers. Thank you though. Commented Mar 14, 2013 at 0:54
  • This answer might be helpful. Commented Mar 14, 2013 at 23:50

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.