字符串索引超出范围:0
问题描述:
在寻找解决方案时,我发现了一个使用sc.next()而不是sc.nextLine()的建议,但我无法使用它,因此我需要额外的帮助。字符串索引超出范围:0
我正在开发一个在线多人游戏文本游戏,需要过滤玩家的名字,但我不断收到错误“字符串索引超出范围: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(名称):
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();
}
我会greatfull任何帮助
答
我想你问题是这样的:
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
总是要么不""
或不" "
。
错误消息应该告诉你错误在哪一行。请让我们知道它是什么(和_show_我是哪一行;不要告诉我们行号,因为我们将无法确定哪一行是最好的,最好是编辑你的问题,并添加一个对您的代码发表评论,说明错误消息指的是哪一行)。 – ajb
'name'的价值是什么?如果错误在'filter()'中,那么其他代码对你的问题没有任何意义,只是噪声。相关部分是导致错误的值和发生错误的行。 – Andreas
不知道错误在哪一行,很难说清楚。但我的猜测是'w'可能是一个空字符串。 – ajb