在寻找解决方案的同时,我发现了使用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(名称):
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();
}
我会很乐意提供任何帮助
答案 0 :(得分: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
始终不是""
或" "
1}}。