Random Nickname Generator

Sungju Kim·2024년 7월 3일

The following Java code generates nick names based on three arrays which contain adjectives and subjects. The key points learning from making this generator was the following:

  • Making an array in Java.
  • Generating random numbers withint a certain range (length of an array) to index the arrays.
  • concatenate the words chosen from each array to make the full nickname.
import java.util.ArrayList;
import java.util.Random;

public class RandomNameGenerator {
    public static void main(String[] args) {
        System.out.println(randomName());
    }

    public static String randomName() {
        // Create lists
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("Adorable");
        list1.add("Beautiful");
        list1.add("Cute");

        ArrayList<String> list2 = new ArrayList<>();
        list2.add("little");
        list2.add("tiny");
        list2.add("gorgeous");

        ArrayList<String> list3 = new ArrayList<>();
        list3.add("baby");
        list3.add("puppy");
        list3.add("bear");

        // Create an instance of Random
        Random random = new Random();

        // Generate random indices and construct the name
        String answer;
        int randomInt1 = random.nextInt(3);
        answer = list1.get(randomInt1) + ' ';
        
        int randomInt2 = random.nextInt(3);
        answer += list2.get(randomInt2);
        answer += ' ';
        
        int randomInt3 = random.nextInt(3);
        answer += list3.get(randomInt3);

        return answer;
    }
}

Static Functions (side note)

Helpful YouTube Video

  • non-static methods can only be called by class objects not the class itself.
  • static variables in a class cannot be reset/redefined through objects of that class (e.i. catCount)
profile
Fully ✨committed✨ developer, always eager to learn!

0개의 댓글