HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Simple Nick Namer

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simplenicknamer

Problem

This is very simple: the user inputs his/her name, the program then truncates it on a consonant at a specified position, if possible if not it will truncate it on a vowel. It then adds an extension from a list of extensions to make the complete nickname and displays it.

/**
 * @author              :KyleMHB
 * Project Number       :0001
 * Project Name         :Nick Namer
 * IDE                  :NETBEANS
 * Goal of Project      -
 * Use a given name and create a nick name using an extension.
 */
package nicknamer;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import javax.swing.JOptionPane;


public class NickNamer {

public static void main(String[] args) {
    String word = getName("Enter your name!");
    List nickExtension =  Arrays.asList("anosorus","asaur","asaurus","onosorus","apede");
    String output=getRandomNick(truncateName(word,"AEIOUaeiou",2),nickExtension);
    showOutput(word+" your Nickname is "+output);
}


private static String getName(String prompt){
    String input=JOptionPane.showInputDialog(null,prompt);
    return input;
}


This method below is meant to truncate the name on a consonant, at a position of my choosing (via the position int) or until it finds one after that position. E.g.: James becomes Jam and Jaemes becomes Jaem.

private static String truncateName(String word,String vowels, int position) {
    char[] wordArray = word.toCharArray();
    String output;
    int iterate=position;
    while(iterate!=word.length()){
    if(vowels.indexOf(wordArray[iterate]) < 0){
        return word.substring(0,iterate+1);  
    }else{iterate++;}
    }
    return word.substring(0,position);
}


private static String getRandomNick(String word,List nickExtension) {
    Random r = new Random();
    return word + nickExtension.get (r.nextInt(nickExtension.size()));
}


private static void showOutput(String output) {
    JOptionPane.showMessageDialog(null,output);
}
}

Solution

private static String getName(String prompt){
    String input=JOptionPane.showInputDialog(null,prompt);
    return input;
}


I like what you've done here. But I would rename the method to getInput(String prompt) since you've made it reusable. You can get anything from the user with this method, since you're passing in the prompt! I would also make it a one-liner instead of storing the variable for no reason.

i.e.,

private static String getInput(String prompt) {
    return JOptionPane.showInputDialog(null, prompt);
}


Now in this method ...

private static String getRandomNick(String word,List nickExtension) {
    Random r = new Random();
    return word + nickExtension.get (r.nextInt(nickExtension.size() - 0));
}


The Random should probably be a constant in your overarching class. Meaning it should be private static final Random RANDOM = new Random(). There's no need to create a new object each time you want to generate a number.

Even if you wanted to do it this way, there would be no reason to store it in a variable.

Also, why are you doing nickExtension.size() - 0? Get rid of the - 0 since it does nothing.

private static String truncateName(String word,String vowels, int position) {
    char[] wordArray = word.toCharArray();
    String output;
    int iterate=position;
    while(iterate!=word.length()){
    if(vowels.indexOf(wordArray[iterate]) < 0){
        return word.substring(0,iterate+1);  
    }else{iterate++;}
return word.substring(0,position);
}


This entire method is really confusing. It looks like you're trying to return a substring of the name when you hit a vowel? But this would do the opposite and actually only continue as long as it finds vowels, since indexOf() will return -1 (and thus be less than 0) if the letter is not present. So as soon as you hit a consonant, your method will return. If this is actually what you were trying to achieve, then I don't get it.

I would rewrite the whole thing. Also, since the vowels are a constant and would never change, I would put that at the top as a constant field as well: private static final List VOWELS = Arrays.asList('a','e','i','o','u','A','E','I','O','U'); There's also no need to pass in the position either, since it's a constant.

private static String truncateName(String name) {
    char[] characters = name.toCharArray();
    for(int i = 3; i < characters.length; i++) {
        if(!VOWELS.contains(Character.toLowerCase(characters[i]))) {
            return name.substring(0, i + 1);
        }
    }
    return name.substring(0, 3);
}


Throw in some general renaming of variables and methods to be more descriptive, cleanup of tabs, indentation, and spaces, clearing of unused variables ...

AND IN THE END, THERE WAS CODE

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import javax.swing.JOptionPane;

public class NickNamer {

    private static final Random RNG = new Random();
    private static final List NICKNAME_EXTENSIONS = Arrays.asList("anosaurus", "asaur", "asaurus", "onosaurus", "apede");
    private static final List VOWELS = Arrays.asList('a', 'e', 'i', 'o', 'u');

    public static void main(String[] args) {
        String name = getInput("Enter your name!");
        String output = getRandomNickname(truncateName(name));
        displayOutput(name + ", your nickname is " + output + "!");
    }

    private static String getInput(String prompt){
        return JOptionPane.showInputDialog(null, prompt);
    }

    private static String truncateName(String name) {
        char[] characters = name.toCharArray();
        for(int i = 3; i < characters.length; i++) {
            if(!VOWELS.contains(Character.toLowerCase(characters[i]))) {
                return name.substring(0, i + 1);
            }
        }
        return name.substring(0, 3);
    }

    private static String getRandomNickname(String word) {
        return word + NICKNAME_EXTENSIONS.get(RNG.nextInt(NICKNAME_EXTENSIONS.size()));
    }

    private static void displayOutput(String output) {
        JOptionPane.showMessageDialog(null,output);
    }

}

Code Snippets

private static String getName(String prompt){
    String input=JOptionPane.showInputDialog(null,prompt);
    return input;
}
private static String getInput(String prompt) {
    return JOptionPane.showInputDialog(null, prompt);
}
private static String getRandomNick(String word,List nickExtension) {
    Random r = new Random();
    return word + nickExtension.get (r.nextInt(nickExtension.size() - 0));
}
private static String truncateName(String word,String vowels, int position) {
    char[] wordArray = word.toCharArray();
    String output;
    int iterate=position;
    while(iterate!=word.length()){
    if(vowels.indexOf(wordArray[iterate]) < 0){
        return word.substring(0,iterate+1);  
    }else{iterate++;}
return word.substring(0,position);
}
private static String truncateName(String name) {
    char[] characters = name.toCharArray();
    for(int i = 3; i < characters.length; i++) {
        if(!VOWELS.contains(Character.toLowerCase(characters[i]))) {
            return name.substring(0, i + 1);
        }
    }
    return name.substring(0, 3);
}

Context

StackExchange Code Review Q#32240, answer score: 3

Revisions (0)

No revisions yet.