【问题标题】:String index out of range: 0字符串索引超出范围:0
【发布时间】:2016-05-15 02:42:47
【问题描述】:

在寻找解决方案时,我发现了使用 sc.next() 而不是 sc.nextLine() 的建议,但我不能使用它,所以我需要额外的帮助。

我正在开发一个在线多人文本游戏,需要过滤玩家姓名,但我不断收到错误“字符串索引超出范围:0”我已经尝试了几个小时来解决这个问题,但未能找到解决方案。 导致错误的函数是这样的:

public static Player charCreate(Player player) {

    int points = 60;
    int i = 0;
    boolean cont = true;
    int str = 0;
    int dex = 0;
    int end = 0;
    int INT = 0;
    int lck = 0;
    int cha = 0;

    Output.slowDiscription("You stand there and think about all"
            + " that has transpired, who are you realy?");

    System.out.print("What is your Name? ");

    boolean bool = false;
    String temp;
    String name = null; //name is forced to be declared by while loop

    do {

        name = sc.nextLine();
        System.out.println(name);
        if (Utills.profan(name) && (name != null)
                && ((!name.equals("")) || (!name.equals(" ")))) {

            bool = true;
            player.setName(Utills.filter(name)); //Error is here

        }
        else {

            bool = false;

        }

    } while (bool == false);

    player.Height = getUsrHeight();

    System.out.print("Please select your stats. ");

    do {

        System.out.println("Points Remaining: " + points);

        switch (i) {

        case 0:

            System.out.println();
            System.out.println("Please Enter the number of points to alot to "
                    + "Strength.\n Min:1 Max:18");

            str = sc.nextInt();
            if ((str >= 1) && (str <= 18) && (str <= points) &&
                    ((points - str) >= 5)) {

                points -= str;
                i++;

            }
            break;

        case 1:

            System.out.println();
            System.out.println("Please Enter the number of points to alot to "
                    + "Dexterity.\n Min:1 Max:18");

            dex = sc.nextInt();
            if ((dex >= 1) && (dex <= 18) && (dex <= points) &&
                    ((points - dex) >= 4)) {

                points -= dex;
                i++;

            }
            break;

        case 2:

            System.out.println();
            System.out.println("Please Enter the number of points to alot to "
                    + "Endurance.\n Min:1 Max:18");

            end = sc.nextInt();
            if ((end >= 1) && (end <= 18) && (end <= points) &&
                    ((points - end) >= 3)) {

                points -= end;
                i++;

            }
            break;

        case 3:

            System.out.println();
            System.out.println("Please Enter the number of points to alot to "
                    + "Inteligence.\n Min:1 Max:18");

            INT = sc.nextInt();
            if ((INT >= 1) && (INT <= 18) && (INT <= points) &&
                    ((points - INT) >= 2)) {

                points -= INT;
                i++;

            }
            break;

        case 4:

            System.out.println();
            System.out.println("Please Enter the number of points to alot to "
                    + "Luck.\n Min:1 Max:18");

            lck = sc.nextInt();
            if ((lck >= 1) && (lck <= 18) && (lck <= points) &&
                    ((points - lck) >= 1)) {

                points -= lck;
                i++;

            }
            break;

        case 5:

            System.out.println();
            System.out.println("Please Enter the number of points to alot to "
                    + "Charisma.\n Min:1 Max:18");

            cha = sc.nextInt();
            if ((cha >= 1) && (cha <= 18) && (cha <= points)) {

                points -= cha;
                i++;

            }
            break;

        case 6:

            int[] stats = {str, dex, end, INT, lck, cha};

            player.setStats(stats);
            cont = false;
            break;

        }

    }while (cont);


    return player;

}

这里的错误来自 Utills.filter(name):

public static String filter(String name) {

    //Variables
    String[] Name = name.toLowerCase().split(" ");

    StringBuilder sb = new StringBuilder();
    StringBuilder endStr = new StringBuilder();

    char temp;

    int i = 0;

    //Sorting
    for(String w: Name) {

        sb.append(w);

        temp = Character.toUpperCase(sb.charAt(0)); //And Error is here
        sb.setCharAt(0, temp);

        if(i >= 1) {

            endStr.append(" " + sb.toString());

        }
        else {

            endStr.append(sb);

        }

        i++;
        empty(sb);

    }


    return endStr.toString();

}

如果有任何帮助,我将不胜感激

【问题讨论】:

  • 错误信息应该告诉你错误在哪一行。请让我们知道它是什么(并向我们展示它是哪一行;不要只告诉我们行号,因为我们无法弄清楚那是哪一行。最好的是编辑您的问题并向您的代码添加注释,指示错误消息所指的行)。
  • name 的值是多少?如果错误在filter() 中,那么其他代码对您的问题没有意义,只是噪音。相关部分是导致错误的值和发生错误的行。
  • 不知道错误出现在哪一行,很难说。但我的猜测是w 可能是一个空字符串。
  • 或有前导空格。
  • 添加 cmets 以显示错误来自何处

标签: java arrays sorting arraylist stringbuilder


【解决方案1】:

我认为你的问题是这样的:

if (Utills.profan(name) && (name != null)
            && ((!name.equals("")) || (!name.equals(" "))))

应该是这样

if (Utills.profan(name) && (name != null)
            && !name.equals("") && !name.equals(" "))

甚至

if (Utills.profan(name) && (name != null)
            && !name.trim().isEmpty())

如果您仍然不明白我的意思是什么问题,检查((!name.equals("")) || (!name.equals(" "))) 将始终为true,因为name 始终不是"" 或不是" "

【讨论】:

    猜你喜欢
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    相关资源
    最近更新 更多