扫描仪和字符串修改错误

时间:2014-12-08 05:28:22

标签: java string console

有人可以告诉我为什么以下代码会产生以下错误:

at java.lang.String.charAt(String.java:646)
    at NameGenerator.leetSpeakModification(NameGenerator.java:49)
    at NameGenerator.main(NameGenerator.java:23)

每当我输入两个单词的名字(名字和姓氏),然后选择继续并生成另一个随机名称。

import java.util.*;

public class NameGenerator {
    public static void main(String[] args) {
        Random r = new Random();
        Scanner console = new Scanner(System.in);
        String finalName;
        String userInput = "y";
        while (userInput.contains("y") || userInput.contains("Y")) {
            String name = intro(console);
            if (name.contains(" ")) {
                finalName = leetSpeakModification(name.substring(0, name.indexOf(' ')), r) + " ";
                finalName += leetSpeakModification(name.substring(name.indexOf(' ')+1, name.length()), r);  
            } else {
                finalName = leetSpeakModification(name, r);
            }
            printName(name, finalName);
            System.out.print("Would you like to generate another name? Please type yes or no. ");
            userInput = console.next();
        }
        System.out.println("Thank you for using the name generator!");
    }

    public static String intro(Scanner console) { //prints intro and asks user for name
        System.out.println("Hello, welcome to the Random Name Generator Program. This program");
        System.out.println("will generate a random name for you using four different");
        System.out.println("modifications.");
        System.out.println();
        System.out.print("Please enter at least a first name to modify: ");
        String name = console.nextLine();
        return name;
    }

    public static String leetSpeakModification(String name, Random r) { //replace letter with
        //leetspeak letters and then calls the rest of the methods to modify name
        name = name.replace("o", "0");
        name = name.replace("l", "1");
        name = name.replace("e", "3");
        name = name.replace("a", "4");
        name = name.replace("t", "7");
        if (name.charAt(name.length()-1) == 's') {
            name = name.substring(0, name.length()-1) + "Z";
        }
        String modifiedName1 = endingModification(name, r);
        String modifiedName2 = colorNumberModification(modifiedName1, r); 
        return modifiedName2;
    }

    public static String endingModification(String name, Random r) { 
        //adds vowel and /"y/" to the end
        String alphabet = "aeiou";
        int random = r.nextInt(5);
        name += alphabet.charAt(random) + "y";
        return name;
    }

    public static String colorNumberModification(String name, Random r) { //adds color name to end
        //and adds letter of the alphabet to beginning
        name += r.nextInt(1000);
        int repeat = r.nextInt(10);
        String alphabet = "abcdefghijklmnopqrstuvwxyz";
        for (int i = 1; i <= repeat; i++) {
            name = alphabet.charAt(r.nextInt(26)) + name;
        }
        int color = r.nextInt(7);
        if (color == 0) {
            name = "red" + name;
        } else if (color == 1) {
            name = "blue" + name;
        } else if (color == 2) {
            name = "green" + name;
        } else if (color == 3) {
            name = "white" + name;
        } else if (color == 4) {
            name = "black" + name;
        } else if (color == 5) {
            name = "purple" + name;
        } else if (color == 6) {
            name = "orange" + name;
        } else {
            name = "yellow" + name;
        }
        return name;
    }

    public static void printName(String originalName, String finalName) { //prints the name out
        System.out.println("Your modified name \"" + originalName + "\" is: " + finalName);
    }
}

0 个答案:

没有答案