0

While looking for a solution to this I found a suggestion to use sc.next() instead of sc.nextLine() but I can't use that so I require additional assistance.

I Am developing an online multiplayer text game and need to filter player name but I keep getting the error "String index out of range: 0" I have tried for hours to fix this and have not been able to find a solution. The Function causing the error is this:

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;

}

The Error here comes from the 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();

}

I would be greatfull for any help

  • The error message should tell you what line the error is on. Please let us know what it is (and _show_ us which line it is; don't just tell us the line number, because we won't be able to figure out which line that is. Best is to edit your question and add a comment to your code indicating what line the error message is referring to). – ajb May 15 '16 at 02:48
  • What is the value of `name`? If the error is in `filter()`, then the other code has no meaning to your question, and is just noise. The relevant part is the value causing the error and the line where the error occurs. – Andreas May 15 '16 at 02:48
  • 1
    Without knowing which line the error is on, it's hard to tell. But my guess would be that `w` may be an empty string. – ajb May 15 '16 at 02:50
  • Or has leading space. – Andreas May 15 '16 at 02:50
  • Added comments to show where error is coming from – Lucian Farsky May 15 '16 at 02:51
  • Also, value of name is determined by the user, "sc" is a static scanner object – Lucian Farsky May 15 '16 at 02:52
  • But what is the value of `name` when the error occurs? What input is causing your problem? – Andreas May 15 '16 at 02:53
  • It throws an error before I can even enter any input – Lucian Farsky May 15 '16 at 02:54
  • What do you mean? You said the error comes on a lines after the `name = sc.nextLine()` statement, which will stop and wait for input. You even have a print statement right after. Or did you use `sc` before, e.g. calling `nextInt()`? – Andreas May 15 '16 at 02:56
  • Define before. The error occurs after that statement, but I do not get a chance to enter anything, it doesn't wait. – Lucian Farsky May 15 '16 at 03:02
  • The Submited solution has solved the problem – Lucian Farsky May 15 '16 at 03:04

1 Answers1

1

I think your problem is this:

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

It should probably be

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

or even

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

If you still don't see what problem I mean, the check ((!name.equals("")) || (!name.equals(" "))) will always be true, because name is always either not "" or not " ".

Vampire
  • 35,631
  • 4
  • 76
  • 102